C# 获取响应流(ReadDone2)时出错:接收失败
请帮帮我。 发送post查询后,我遇到webexception“获取响应流时出错(ReadDone2):接收失败”。帮助消除此错误。谢谢 一段代码C# 获取响应流(ReadDone2)时出错:接收失败,c#,xamarin,xamarin.android,C#,Xamarin,Xamarin.android,请帮帮我。 发送post查询后,我遇到webexception“获取响应流时出错(ReadDone2):接收失败”。帮助消除此错误。谢谢 一段代码 try { string queryContent = string.Format("login={0}&password={1}&mobileDeviceType={2}/", login, sessionPassword, deviceType); request = ConnectionHelper.GetHttpWebRequ
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;
}
//方法获取HttpWebRequest
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;
}
在我的例子中,服务器没有发送响应正文。修复服务器后,“接收失败”消失 所以你有两个选择:
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()
它不是Xamarin或.NET的bug眨眼: 服务器端终结点在请求时失败。:中性: 如果您拥有API,请检查您的API端点,如果您从第三方公司或服务获得API,请与支持部门联系 随机发生的原因::wink: 因为bug位于内部if或循环块中,当它通过这个bug循环或if时就会发生
致以最良好的祝愿微笑:您可以发布ConnectionHelper类(或者可能只是GetHttpWebRequest方法)的代码吗?使用“using”关键字时是否需要显式调用Close()函数?我认为当流超出“using”语句的范围时,它会自动释放/关闭。我也这么认为,但在practice without.Close()上,它不会发送请求。
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write("Thanks!\n")
self.wfile.close()