HttpWebRequest getRequestStream hangs on multiple

2019-04-20 19:35发布

问题:

I've written some code to send and read text from a listener. This runs fine on the 1st and 2nd exchange, but on the 3rd send there's a long delay between calling GetRequestStream and the actual writing of the data.

I've disposed the outstream on the send side, as well as the stream reader, and the input stream on the read side as recommended here : Does anyone know why I receive an HttpWebRequest Timeout?

and it still hangs on the 3rd attempt to send info. It definitely seems to be hanging at GetRequestStrean in sendMessage :

public void sendMessage(string message)
{
    HttpWebRequest request;
    string sendUrl;

    sendUrl = "http://" + termIPAddress + ":" + sendPort + "/";
    Uri uri = new Uri(sendUrl);
    Console.WriteLine("http://" + termIPAddress + ":" + sendPort + "/");

    ServicePoint servicePoint = ServicePointManager.FindServicePoint(uri);
    servicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);
    servicePoint.ConnectionLeaseTimeout = 300;


    request = (HttpWebRequest)WebRequest.Create(sendUrl);
    request.KeepAlive = false;
    request.Method = "POST";
    request.ProtocolVersion = HttpVersion.Version11;
    request.ContentType = "application/x-www-form-urlencoded";
    request.Headers.Add("SourceIP", localIPAddress);
    request.Headers.Add("MachineName", localName);
    requestStarted = true;


    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message);
    request.ContentLength = buffer.Length;
    try
    {
        using (Stream output = request.GetRequestStream())
        {
            output.Write(buffer, 0, buffer.Length);
            output.Close();
            request = null;
        }
    }
    catch(WebException wE)
    {
        Console.WriteLine(wE.Message);
    }
}

And this is the read portion :

public string getMessage()
{
    Console.WriteLine("Entering actual listener");
    string s;
    string sourceIP;

    NameValueCollection headerList;

    HttpListenerContext context = terminalListener.GetContext();
    HttpListenerRequest request = context.Request;

    headerList = request.Headers;
    sourceIP = headerList.GetValues("SourceIP")[0];
    termName = headerList.GetValues("MachineName")[0];
    termIPAddress = sourceIP;
    using (System.IO.Stream body = request.InputStream)
    {
        System.Text.Encoding encoding = request.ContentEncoding;
        using (System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding))
        {
            s = reader.ReadToEnd();
            body.Close();
            reader.Close();
        }
    }

    return termName + " : " + s;    
}

I also tried to add an IP End Point bind but have to be honest, I don't fully understand this piece of the code :

private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
{
    int portNumber = Convert.ToInt32(sendPort);
    IPEndPoint IEP = new IPEndPoint(IPAddress.Parse(localIPAddress), 0); 
    Console.WriteLine(Convert.ToString(IEP));
    return IEP;  
}

Any help greatly appreciated.

回答1:

You just forgot to call HttpWebRequest.GetResponse and therefore run out of connection limit.

So, you should change your code as follows:

try
{
    using (Stream output = request.GetRequestStream())
        output.Write(buffer, 0, buffer.Length);

    var response = request.GetResponse() as HttpWebResponse;
    //TODO: check response.StatusCode, etc.
}
catch(WebException wE)
{
    Console.WriteLine(wE.Message);
}

Also, in some cases you might want to adjust default connection limit: ServicePointManager.DefaultConnectionLimit

Or use persistent connections: HttpWebRequest.KeepAlive