Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7541025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:47:13+00:00 2026-05-30T07:47:13+00:00

I created simple HttpListener that listens to port 9090 and depending on a request’s

  • 0

I created simple HttpListener that listens to port 9090 and depending on a request’s URL writes some info to the console.

But I’m stuck 🙁 I thought of multithreading, event based system, but I dind’t manage to do anything with it.

Here is the code of my listener that I launched as a separate console app:

string urlTemplate = String.Format("/prefix/{0}/suffix", id);
string prefix = String.Format("http://localhost:9090/");

HttpListener listener = new HttpListener();
listener.Prefixes.Add(prefix);

listener.Start();

Console.WriteLine("Listening to {0}...", prefix);

while (true)
{
    HttpListenerContext context = listener.GetContext();
    HttpListenerRequest request = context.Request;

    //Response object
    HttpListenerResponse response = context.Response;

    //Construct response
    if (request.RawUrl.Contains(urlTemplate) && request.HttpMethod == "POST")
    {
         string requestBody;
         Stream iStream = request.InputStream;
         Encoding encoding = request.ContentEncoding;
         StreamReader reader = new StreamReader(iStream, encoding);
         requestBody = reader.ReadToEnd();

         Console.WriteLine("POST request on {0} with body = [{1}]", request.RawUrl, requestBody);

         response.StatusCode = (int)HttpStatusCode.OK;

         //Return a response
         using (Stream stream = response.OutputStream) { }
     }
     else
     {
         response.StatusCode = (int)HttpStatusCode.BadRequest;
         Console.WriteLine("Invalid HTTP request: [{0}] {1}", request.HttpMethod, request.Url);
         using (Stream stream = response.OutputStream) { }
     }
}

I decided to use it as an utility for unit tests (maybe somewhere else). So when test starts I need to configure the listener, run it, then make some requests and receive the info (which listener wrote earlier to Console), and at the end of the test stop the listener.

My main idea was to incapsulate this listener to separate class MyHttpListener which has methods: StartListener(), StopListener().

But when I call StartListener() my test freezes because of infinite while loop. I tried to create separate background thread or event based system, but my lack of experience with them, prevents me from doing it. I’ve already spent a lot of time trying to find the solution, but all for nothing.

Hope you can help me finding the solution for such trivial task.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:47:14+00:00Added an answer on May 30, 2026 at 7:47 am

    One of the responder’s variant (it seems he deleted his post) looked good, but it didn’t work for me. I tried to fix things in order it started working, but at the end that variant gave me an idea how to solve the problem – with multithreading and events 🙂

    Here’s what I have now and it works:

    public delegate void HttpListenerRequestHandler(object sender, HttpListenerEventArgs e);
    public event HttpListenerRequestHandler OnCorrectRequest;
    

    …

    if(OnCorrectRequest != null)
        OnCorrectRequest(this, new HttpListenerEventArgs(response));
    lock (threadLock)
    {
        Console.WriteLine("POST request on {0} with body = [{1}]", request.RawUrl, requestBody);
    }
    

    …

    public class HttpListenerEventArgs : EventArgs
    {
        public readonly HttpListenerResponse response;
        public HttpListenerEventArgs(HttpListenerResponse httpResponse)
        {
            response = httpResponse;
        }
    }
    

    In order to receive detailed responses from HttpListener in main thread, I use the following:

    private HttpListenerResponse response;
    
    public void HttpCallbackRequestCorrect(object sender, HttpListenerEventArgs e)
    {
        response = e.response;
        Console.WriteLine("{0} sent: {1}", sender, e.response.StatusCode);
    }
    

    Unfortunately I have the following exception which I don’t know how to handle:

    System.Net.HttpListenerException: The I/O operation has been aborted because of either a thread exit or an application request
    at System.Net.HttpListener.GetContext()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created simple application that monitors X11's _NET_CLIENT_LIST . It prints me info when
I have created simple web service for my website that generates some json based
I am new to Repository concept and get some questions. I have created simple
I created a simple tooltip for some buttons in an admin panel I am
Have created simple Ajax enabled contact forms before that have around 12 fields -
I have created simple Java class to test file writes from applets: update appeared
I have created simple .aspx page which queries a database for some live data
I created simple modal view photo gallery that gets picture where user clicked and
I created simple REST web app that would create Excel file and put into
I created a simple application that shows add using ad whirl .it does not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.