Click here to Skip to main content
15,881,803 members
Home / Discussions / C#
   

C#

 
GeneralRe: CsvHelper, EBay Order Csv file, not sure how to handle header with quotes and first line of commas Pin
Dave Kreskowiak3-Sep-20 8:51
mveDave Kreskowiak3-Sep-20 8:51 
AnswerRe: CsvHelper, EBay Order Csv file, not sure how to handle header with quotes and first line of commas PinPopular
Richard Deeming2-Sep-20 22:50
mveRichard Deeming2-Sep-20 22:50 
GeneralRe: CsvHelper, EBay Order Csv file, not sure how to handle header with quotes and first line of commas Pin
jkirkerx3-Sep-20 6:13
professionaljkirkerx3-Sep-20 6:13 
AnswerRe: CsvHelper, EBay Order Csv file, not sure how to handle header with quotes and first line of commas Pin
Luc Pattyn3-Sep-20 1:36
sitebuilderLuc Pattyn3-Sep-20 1:36 
GeneralRe: CsvHelper, EBay Order Csv file, not sure how to handle header with quotes and first line of commas Pin
jkirkerx3-Sep-20 6:12
professionaljkirkerx3-Sep-20 6:12 
AnswerRe: CsvHelper, EBay Order Csv file, not sure how to handle header with quotes and first line of commas Pin
jkirkerx3-Sep-20 6:31
professionaljkirkerx3-Sep-20 6:31 
AnswerRe: CsvHelper, EBay Order Csv file, not sure how to handle header with quotes and first line of commas Pin
Gerry Schmitz3-Sep-20 10:23
mveGerry Schmitz3-Sep-20 10:23 
QuestionConnect POST server call with HttpListener C# Pin
jdamiancabello2-Sep-20 0:21
jdamiancabello2-Sep-20 0:21 
Hey guys i'm trying to handle a server request by console app. I have a Listener class that implements a delegate and do some stuff when receive the request.

Now im getting calls by postman in localhost, and don't know how to implement to get server calls (idk if need other application for this like dotnet server or idk).

The calls is based in the appClientCode so i need to get only the client call can be more clients on but they din't notice about other calls.

How can i manage that? Code below.

Anyway thx.


Listener class:
C#
<pre>using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;

namespace MyListener
{
    public delegate byte[] ProcessDataDelegate(string data);

    class MyListener
    {
        private const int HandlerThread = 2;
        private readonly ProcessDataDelegate handler;
        private readonly HttpListener listener;

        public MyListener(HttpListener listener, string url, ProcessDataDelegate handler)
        {
            this.listener = listener;
            this.handler = handler;
            listener.Prefixes.Add(url);
        }

        public void Start()
        {
            if (listener.IsListening)
                return;

            listener.Start();

            for (int i = 0; i < HandlerThread; i++)
            {
                listener.GetContextAsync().ContinueWith(ProcessRequestHandler);
            }
        }

        public void Stop()
        {
            if (listener.IsListening)
                listener.Stop();
        }

        private void ProcessRequestHandler(Task<HttpListenerContext> result)
        {
            try
            {

                if (!listener.IsListening)
                    return;

                listener.GetContextAsync().ContinueWith(ProcessRequestHandler);

                string request = new StreamReader(result.Result.Request.InputStream).ReadToEnd();

                var responseBytes = handler.Invoke(request);
                result.Result.Response.ContentLength64 = responseBytes.Length;

                var output = result.Result.Response.OutputStream;
                output.WriteAsync(responseBytes, 0, responseBytes.Length);
                output.Close();
            }
            catch (Exception)
            {

            }
        }
    }
}


Main class (Console):
C#
using System;
using System.Collections.Generic;
using System.Net;

namespace MyListener
{
    class Program
    {
        static MyListener myListener;
        static bool bussy = false;
        static void Main(string[] args)
        {
            Console.WriteLine("Listening...");
            StartListener("1");
            Console.ReadKey();
        }

        public static void StartListener(string clientCode)
        {
            var httpListener = new HttpListener();
            myListener = new MyListener(httpListener, string.Format("{0}{1}/", "http://127.0.0.1/", clientCode), ProcessYourResponse);

            myListener.Start();
        }

        public static byte[] ProcessYourResponse(string requestParams)
        {
            if (bussy)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Call end[{0}]\n\n", DateTime.Now.ToString());
                Console.ResetColor();
                bussy = false;
                return new byte[0];
            }
            Dictionary<string, string> responseData = MakeNewDictionaryFromResponse(requestParams);
            bussy = true;
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("New call [{0}]:", DateTime.Now.ToString()) ;
            Console.ResetColor();
            Console.WriteLine("Key                                           Value");
            Console.WriteLine("=====================================================================================");
            foreach (KeyValuePair<string, string> entry in responseData)
            {
                Console.WriteLine("{0}{1}{2}",entry.Key.PadRight(16), " ".PadRight(30),entry.Value);
            }
            return new byte[0];
        }

        private static Dictionary<string, string> MakeNewDictionaryFromResponse(string requestParams)
        {
            string[] splitParams = requestParams.Split('&');
            Dictionary<string, string> aux = new Dictionary<string, string>();

            foreach (var item in splitParams)
            {
                string[] tmp = item.Split('=');
                aux.Add(tmp[0], tmp[1]);
            }

            return aux;
        }
    }
}

AnswerRe: Connect POST server call with HttpListener C# Pin
Afzaal Ahmad Zeeshan3-Sep-20 5:19
professionalAfzaal Ahmad Zeeshan3-Sep-20 5:19 
QuestionInheritance problem Pin
Croccodillo19711-Sep-20 5:22
Croccodillo19711-Sep-20 5:22 
AnswerRe: Inheritance problem Pin
Richard Deeming1-Sep-20 7:26
mveRichard Deeming1-Sep-20 7:26 
GeneralRe: Inheritance problem Pin
Croccodillo19711-Sep-20 21:58
Croccodillo19711-Sep-20 21:58 
AnswerRe: Inheritance problem Pin
BillWoodruff9-Sep-20 21:16
professionalBillWoodruff9-Sep-20 21:16 
QuestionHow to reliably exit Outlook using Interop.Outlook? Pin
Server Automator30-Aug-20 14:43
professionalServer Automator30-Aug-20 14:43 
AnswerRe: How to reliably exit Outlook using Interop.Outlook? Pin
Gerry Schmitz31-Aug-20 4:50
mveGerry Schmitz31-Aug-20 4:50 
AnswerRe: How to reliably exit Outlook using Interop.Outlook? Pin
Dave Kreskowiak31-Aug-20 12:12
mveDave Kreskowiak31-Aug-20 12:12 
AnswerRe: How to reliably exit Outlook using Interop.Outlook? Pin
Mycroft Holmes31-Aug-20 12:15
professionalMycroft Holmes31-Aug-20 12:15 
Questionfind TIMES in string of text Pin
free-pizza30-Aug-20 13:52
free-pizza30-Aug-20 13:52 
AnswerRe: find TIMES in string of text Pin
Richard Andrew x6430-Aug-20 15:03
professionalRichard Andrew x6430-Aug-20 15:03 
GeneralRe: find TIMES in string of text Pin
free-pizza30-Aug-20 16:26
free-pizza30-Aug-20 16:26 
AnswerRe: find TIMES in string of text Pin
OriginalGriff30-Aug-20 19:47
mveOriginalGriff30-Aug-20 19:47 
JokeRe: find TIMES in string of text Pin
Peter_in_278030-Aug-20 19:59
professionalPeter_in_278030-Aug-20 19:59 
GeneralRe: find TIMES in string of text Pin
OriginalGriff30-Aug-20 20:08
mveOriginalGriff30-Aug-20 20:08 
GeneralRe: find TIMES in string of text Pin
OriginalGriff30-Aug-20 20:11
mveOriginalGriff30-Aug-20 20:11 
GeneralRe: find TIMES in string of text Pin
Peter_in_278030-Aug-20 20:43
professionalPeter_in_278030-Aug-20 20:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.