【问题标题】:Error getting response stream (ReadDone2): Receive Failure获取响应流时出错 (ReadDone2):接收失败
【发布时间】:2011-11-09 04:53:39
【问题描述】:

请帮帮我。 发送后查询后,我有网络异常“获取响应流时出错(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;
    }

【问题讨论】:

  • 你能发布 ConnectionHelper 类的代码(或者只是 GetHttpWebRequest 方法)吗?

标签: c# xamarin xamarin.android


【解决方案1】:
using (Stream newStream = request.GetRequestStream())
{
    newStream.Write(SomeBytes, 0, SomeBytes.Length);

    //try to add
    newStream.Close();
}

【讨论】:

  • 使用'using'关键字时是否需要显式调用Close()函数?我认为当流超出“使用”语句的范围时,它会自动释放/关闭。
  • 我也这么认为,但在实践中没有 .Close() 它不会发送请求。
【解决方案2】:

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

所以你有两个选择:

  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()
    

【讨论】:

    【解决方案3】:

    它的不是 Xamarin 或.NET 的错误。 :wink:

    您的服务器端端点在请求时失败。 :neutral:

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

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

    最好的问候。 :微笑:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 2013-02-17
      • 2013-02-26
      相关资源
      最近更新 更多