6

请帮帮我。发送后查询后,我有网络异常“获取响应流时出错(ReadDone2):接收失败”。帮助摆脱这个错误。谢谢。

一段代码

try
{
string queryContent = string.Format("login={0}&password={1}&mobileDeviceType={2}/",
login, sessionPassword, deviceType);
request = ConnectionHelper.GetHttpWebRequest(loginPageAddress, queryContent);

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())//after this line //occurs exception - "Error getting response stream (ReadDone2): Receive Failure"
{

ConnectionHelper.ParseSessionsIdFromCookie(response);

string location = response.Headers["Location"];
if (!string.IsNullOrEmpty(location))
{
string responseUri = Utils.GetUriWithoutQuery(response.ResponseUri.ToString());
string locationUri = Utils.CombineUri(responseUri, location);
result = this.DownloadXml(locationUri);
}
response.Close();
}
}
catch (Exception e)
{
errorCout++;
errorText = e.Message;
}

//方法GetHttpWebRequest

    public static HttpWebRequest GetHttpWebRequest(string uri, string queryContent)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);            
        request.Proxy = new WebProxy(uri);
        request.UserAgent = Consts.userAgent;
        request.AutomaticDecompression = DecompressionMethods.GZip;
        request.AllowWriteStreamBuffering = true;
        request.AllowAutoRedirect = false;

        string sessionsId = GetSessionsIdForCookie(uri);
        if (!string.IsNullOrEmpty(sessionsId))
            request.Headers.Add(Consts.headerCookieName, sessionsId);

        if (queryContent != string.Empty)
        {
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";
            byte[] SomeBytes = Encoding.UTF8.GetBytes(queryContent);
            request.ContentLength = SomeBytes.Length;
            using (Stream newStream = request.GetRequestStream())
            {
                newStream.Write(SomeBytes, 0, SomeBytes.Length);
            }
        }
        else
        {
            request.Method = "GET";
        }

        return request;
    }
4

3 回答 3

0
using (Stream newStream = request.GetRequestStream())
{
    newStream.Write(SomeBytes, 0, SomeBytes.Length);

    //try to add
    newStream.Close();
}
于 2011-09-12T14:59:19.490 回答
0

在我的情况下,服务器没有发送响应正文。修复服务器后,“接收失败”消失了。

所以你有两个选择:

  1. 如果您可以没有它,请不要请求响应流。

  2. 确保服务器发送响应正文。

    例如,而不是

    self.send_response(200)
    self.wfile.close()
    

    Python 服务器代码应该是

    self.send_response(200)
    self.send_header('Content-type', 'text/plain')
    self.end_headers()
    self.wfile.write("Thanks!\n")
    self.wfile.close()
    
于 2015-07-31T13:47:58.990 回答
-2

不是Xamarin 或 .NET 的错误。:眨眼:

请求时您的服务器端端点失败。:中性的:

如果您拥有 API,请检查您的 API 端点;如果您从第三方公司或服务获取 API,请联系支持人员。

为什么它随机发生: :wink: 因为错误在你的内部 if 或循环块中,它发生在它通过这个错误的循环或 if 时。

最好的祝福。:微笑:

于 2018-09-22T10:33:06.483 回答