1

I am using this code to implement Http Server:

public Server()
    {
        _httpListener = new HttpListener();
        _httpListener.Prefixes.Add(Server.UriAddress);
        StartServer();
    }

    public void StartServer()
    {
        _httpListener.Start();

        while (_httpListener.IsListening)
            ProcessRequest();
    }

    void ProcessRequest()
    {
        var result = _httpListener.BeginGetContext(ListenerCallback, _httpListener);
        result.AsyncWaitHandle.WaitOne();
    }

    void ListenerCallback(IAsyncResult result)
    {
        HttpListenerContext context = _httpListener.EndGetContext(result);
        HttpListenerRequest request = context.Request;
        string url = request.RawUrl;
        url = url.Substring(1, url.Length - 1);

        HttpListenerResponse response = context.Response;
        string responseString = url;
        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
        response.ContentLength64 = buffer.Length;
        System.IO.Stream output = response.OutputStream;
        output.Write(buffer, 0, buffer.Length);
        output.Close();
    }

And i have a problem that if i wrote this in the browser(It's an example and it's occur on every call):

http://localhost:8888/Hello%20World

the ListenerCallback method is called twice,any idea how to fix it?

YosiFZ
  • 7,792
  • 21
  • 114
  • 221
  • Your code is fine (I run it on my comp.). Either you don't use this code or your client makes multiple requests. – I4V Apr 11 '13 at 12:38
  • It is normal that chrome and IE make 2 request and Firefox make one request? – YosiFZ Apr 11 '13 at 12:47
  • MTA, I tested it with IE and chrome, both make 1 request. – I4V Apr 11 '13 at 12:49
  • 1
    Put Fiddler in between to see what requests are being made. Are plugins running, is the favicon being requested? – CodeCaster Apr 11 '13 at 13:15

3 Answers3

7

An answer has been accepted already, but I think it may still be useful to other people subsequently.

By default, most browsers given a URL would make at least two calls. One call to the request URL and the other to favicon.ico.

So a check should be made in ListenerCallback like

HttpListenerContext context = _httpListener.EndGetContext(result);
HttpListenerRequest request = context.Request;
string url = request.RawUrl;
url = url.Substring(1);
if (url == "favicon.ico")
{
    return;
}
//normal request handling code

I hope this helps someone.

Omoloro
  • 342
  • 1
  • 3
  • 9
2

If your website requires several calls to the server, it will be called several times. This happens when you hav pictures or anything else on you page.
Try to call the synchronous method _httpListener.GetContext() or synchronize your calls with a lock or Mutex.

bash.d
  • 13,029
  • 3
  • 29
  • 42
  • The problem is that i need to send to the server a command to download a file and it download the file twice now – YosiFZ Apr 11 '13 at 12:34
  • I am not sure if you are using `IAsyncResult` correctly... Have a look [here](http://msdn.microsoft.com/en-us/library/system.iasyncresult.asyncwaithandle.aspx). – bash.d Apr 11 '13 at 13:14
0

I am not sure but I think I see what might be a problem with your code. You are mixing two patterns of async handling You release the main thread by waiting on the waithandle in the async result. But I think that only signals that you can call endgetcontext, not that another thread can use the listener. If you are using the callback pattern, you should release the main thread using a wait handle other than that provided in the asyncresult

user2849221
  • 113
  • 8