请求已中止:当结果超过长度时,连接意外关闭

时间:2016-10-28 09:02:51

标签: c# asp.net web-services httpwebrequest

我收到此异常"请求已中止:连接意外关闭。"

代码:

private T ConnectAPI<T>(object item)
{
try
{
    object readData = new object();
    HttpWebRequest request = null;
    HttpWebResponse myHttpWebResponse = null;
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    //url: http:api.xxx/yyy?data={convert data to json}
    string url = ConfigurationManager.AppSettings["URL"];
    string param = serializer.Serialize(item);
    byte[] postBytes = Encoding.ASCII.GetBytes(param); 
    request = (HttpWebRequest)WebRequest.Create(url+param);
    request.Method = "POST";
    request.ContentType = "application/json";
    request.ContentLength = postBytes.Length;       
    request.Timeout = 100000;

    using (var stream = request.GetRequestStream())
    {
        stream.Write(postBytes, 0, postBytes.Length);
    }

    //Problem a here
    myHttpWebResponse = (HttpWebResponse)request.GetResponse();

    string target = string.Empty;
    using (StreamReader streamReader = new StreamReader(myHttpWebResponse.GetResponseStream()))
    {
        target = streamReader.ReadToEnd();
        JavaScriptSerializer json_serializer = new JavaScriptSerializer();
        //Return result a here
        readData = json_serializer.Deserialize<T>(target);
        streamReader.Close();
    }

    myHttpWebResponse.Close();
    return (T)readData;
}
catch (WebException e)
{       
    return default(T);
}
}

返回&#34; readData&#34;有{error_code,message,list_object}

问题:如果list_object有10个元素则ok。但是list_object有超过100个元素然后异常:&#34;请求被中止:连接意外关闭。&#34;

0 个答案:

没有答案