Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GetRequestStream抛出超时异常_C#_.net_Multithreading - Fatal编程技术网

C# GetRequestStream抛出超时异常

C# GetRequestStream抛出超时异常,c#,.net,multithreading,C#,.net,Multithreading,我有多线程核心C#应用程序,它使用外部库(也用C#编写)与webservice交互。同步模式下多线程中的核心应用程序从库调用方法。 在负载比通常稍高的情况下,此方法执行时间可达20-40秒或更长,有时会抛出超时异常。这里是代码和标记行,它们引发异常 public static class NetCommunicator { static string MakeHttp(string url, string body) { WebRequest r = WebReq

我有多线程核心C#应用程序,它使用外部库(也用C#编写)与webservice交互。同步模式下多线程中的核心应用程序从库调用方法。 在负载比通常稍高的情况下,此方法执行时间可达20-40秒或更长,有时会抛出超时异常。这里是代码和标记行,它们引发异常

public static class NetCommunicator
{
    static string MakeHttp(string url, string body)
    {
        WebRequest r = WebRequest.Create(url);

        r.Method = body == null ? "GET" : "POST";
        r.Timeout = 60000;

        if (body != null)
        {
            r.ContentType = "text/xml; charset=utf-8";
            byte[] bytes = Encoding.UTF8.GetBytes(body);
            using (Stream s = r.GetRequestStream()) // this line execute up to 20-40 and more sec, and sometimes throw timed out exception
                s.Write(bytes, 0, bytes.Length);
        }

        using (WebResponse httpResp = r.GetResponse())
        using (Stream s = httpResp.GetResponseStream())
        using (StreamReader sr = new StreamReader(s))
            return sr.ReadToEnd();
    }

    public static string MakeHttpPost(string url, string body)
    {
        return MakeHttp(url, body);
    }
}

你的问题是什么?你是否设置了足够高的值?这可能是TCP连接超时。互联网连接不稳定或服务器过载。这是一个系统设置。