HttpWebRequest getRequestStream挂起多次运行 [英] HttpWebRequest getRequestStream hangs on multiple runs

查看:156
本文介绍了HttpWebRequest getRequestStream挂起多次运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一些代码来发送和读取来自侦听器的文本.这在第一个和第二个交换上运行良好,但是在第三个发送上,在调用GetRequestStream和实际写入数据之间存在很长的延迟.

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.

我在这里建议将发送流,流读取器和输入流放在发送侧,并将流放在读取器侧: 有人知道我为什么收到HttpWebRequest超时的原因吗?

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?

,它仍然挂在第三次尝试发送信息上.它肯定似乎挂在sendMessage的GetRequestStrean上:

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);
    }
}

这是读取的部分:

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;    
}

我也尝试添加IP端点绑定,但是老实说,我不完全理解这段代码:

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.

推荐答案

您只是忘记了致电

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);
}

此外,在某些情况下,您可能需要调整默认连接限制: ServicePointManager.DefaultConnectionLimit

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

或使用持久连接: HttpWebRequest.KeepAlive

Or use persistent connections: HttpWebRequest.KeepAlive

这篇关于HttpWebRequest getRequestStream挂起多次运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆