获取响应流时出错 (ReadDone2):接收失败

发布于 2024-12-02 10:19:45 字数 2116 浏览 4 评论 0原文

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

Help me please.
After sending post query i have webexception "Error getting response stream (ReadDone2): Receive Failure". help get rid of this error. thanks.

piece of code

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哆兒滾 2024-12-09 10:19:45
using (Stream newStream = request.GetRequestStream())
{
    newStream.Write(SomeBytes, 0, SomeBytes.Length);

    //try to add
    newStream.Close();
}
using (Stream newStream = request.GetRequestStream())
{
    newStream.Write(SomeBytes, 0, SomeBytes.Length);

    //try to add
    newStream.Close();
}
拥抱我好吗 2024-12-09 10:19:45

就我而言,服务器没有发送响应正文。修复服务器后,“接收失败”消失了。

因此,您有两个选择:

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

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

    例如,而不是

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

    Python 服务器代码应该是

    self.send_response(200)
    self.send_header('内容类型', '文本/纯文本')
    self.end_headers()
    self.wfile.write("谢谢!\n")
    self.wfile.close()
    

In my case the server did not send a response body. After fixing the server, the "Receive Failure" vanished.

So you have two options:

  1. Don't request a response stream, if you can live without it.

  2. Make sure the server sends a response body.

    E.g., instead of

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

    the Python server code should be

    self.send_response(200)
    self.send_header('Content-type', 'text/plain')
    self.end_headers()
    self.wfile.write("Thanks!\n")
    self.wfile.close()
    
彻夜缠绵 2024-12-09 10:19:45

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

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

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

为什么它随机发生: :wink:
因为 bug 存在于您内部的 if 或循环块中,并且当它经过这个有缺陷的循环或 if 时就会发生。

此致。 :微笑:

its not Xamarin or .NET's bug. :wink:

your server side endpoint fails when requesting. :neutral:

check your API endpoint if you own the api or contact support if you are getting API from third party company or service.

why it happens randomly : :wink:
because bug is in your inner if or loop blocks and it happens when it goes through this buggy loop or if.

best regards. :smile:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文