HttpWebRequest和之后的GetResponse 1请求挂起;其他解决方案不适合我 [英] HttpWebRequest and GetResponse hangs after 1 request; other solutions doesn't work for me
问题描述
更新:
我已经找到了,这是不是一个C2DM问题,但有些问题HttpWebRequest和它的GetResponse,请参见下面的注释。
/ END修改
所以我设置为Google的C2DM系统的连接。它的工作原理 - 排序
当我问C2DM为平我的Android设备,它这样做的时候,但是远远没有永远。这个问题显示本身上的事物的服务器侧
通常,第一个平(在服务器启动后)穿过。我得到的回应我所说的互联网服务和不久之后的平显示我的Android设备上之后。
不过,如果我的第一个平后不久,又告诉我的服务器发送一个ping,该Web服务调用失败。它从来没有设法调用C2DM,一段时间后,我得到一个WebException(C#)说以下内容:
基础连接已关闭:发生意外错误 接收。
没有其他的信息是存在的。如果我再尝试重新发送,也即呼叫被封锁。
我改变什么,在code,有时设法调用webservice的,有时它没有。在我看来,有一个超时来调用Web服务谷歌。如果你调用它,你需要再次调用,否则它只是将不会返回答案,并给异常之前等待X秒。那只是一个猜测...
任何想法?
这是在送执行code ...
HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(GoogleMessageUrl); //谷歌的网址
request.Method =POST
request.KeepAlive = FALSE;
NameValueCollection中postFieldNameValue =新的NameValueCollection();
postFieldNameValue.Add(RegistrationIdParam,registrationId); //有效的寄存器ID为Android设备
postFieldNameValue.Add(CollapseKeyParam,0);
postFieldNameValue.Add(DelayWhileIdleParam,0);
postFieldNameValue.Add(DataPayloadParam,消息);
字符串POSTDATA = GetPostStringFrom(postFieldNameValue);
字节[]的字节数组= Encoding.UTF8.GetBytes(POSTDATA);
request.ContentType =应用/的X WWW的形式urlen codeD;字符集= UTF-8;
request.ContentLength = byteArray.Length;
request.Headers.Add(Htt的prequestHeader.Authorization的GoogleLogin AUTH =+ _authTokenString);
流式传输的数据流= request.GetRequestStream();
dataStream.Write(字节数组,0,byteArray.Length);
dataStream.Close();
WebResponse的响应= request.GetResponse();
的HTTPStatus code响应code =((HttpWebResponse)响应).STATUS code;
如果(响应code.Equals(的HTTPStatus code.Unauthorized)||响应code.Equals(的HTTPStatus code.Forbidden))
{
Console.WriteLine(未经授权 - 需要新的令牌);
}
否则,如果(!响应code.Equals(的HTTPStatus code.OK))
{
Console.WriteLine(从Web服务响应不正常:);
Console.WriteLine(((HttpWebResponse)响应).StatusDescription);
}
StreamReader的读者=新的StreamReader(response.GetResponseStream());
字符串responseLine = reader.ReadLine();
reader.Close();
response.Close();
返回responseLine; //它从来没有送过来,但一个例外是在别的地方捕获
哈哈,好像我必须再次回答我的问题=)
所以,我花了过去48个小时的测试,阅读,寻找一个解决HttepWebRequest问题,或者更确切地说,问题的HTTP请求。
我所发现的建议是没有用的我,除了这一个:
<一个href="http://www.eggheadcafe.com/community/vb/14/60113/webclient-to-http-post--error-on-receive.aspx" rel="nofollow">http://www.eggheadcafe.com/community/vb/14/60113/webclient-to-http-post--error-on-receive.aspx
还有就是说,
在互联网上大量的研究后,我偶然建议, 说来设置保活为FALSE,并使用HTTP1.0。
但他同时也指出,它并没有为他工作:
然而,当我尝试使用该功能,我无法成功 无论是机器上执行任何职位 - 我的家中或办公室的PC
但是,它似乎已经完全解决了我的问题!
让我们期待它坚持=)
UPDATED:
I have found out that this is not a C2DM problem, but some problem with HttpWebRequest and its GetResponse, see comments below.
/END EDIT
So I have set up a connection to Googles C2DM system. It works - sort of.
When I ask C2DM to "ping" my Android device, it does so sometimes, but far from always. The problem shows itself on the server-side of things.
Usually, the first "ping" (after the server has started) goes through. I get a response immediately after I call the webservice and shortly thereafter the "ping" shows up on my Android device.
However, if I shortly after the first "ping" again tell my server to send a "ping", the webservice call fails. It never manages to call the C2DM and after a while I get a WebException (C#) saying the following:
The underlying connection was closed: An unexpected error occurred on a receive.
No other information is there. If I then try to send again, also that call is blocked.
I change nothing in the code, sometimes it manages to call the webservice and sometimes it doesnt. It seems to me there is a "timeout" for calling the Google webservice. If you call it, you need to wait for X seconds before calling again otherwise it just won't return an answer and give the exception. Thats just a guess...
Any ideas?
This is the code that executes on send...
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(GoogleMessageUrl); // the google url
request.Method = "POST"
request.KeepAlive = false;
NameValueCollection postFieldNameValue = new NameValueCollection();
postFieldNameValue.Add(RegistrationIdParam, registrationId); // a valid reg id for an android device
postFieldNameValue.Add(CollapseKeyParam, "0");
postFieldNameValue.Add(DelayWhileIdleParam, "0");
postFieldNameValue.Add(DataPayloadParam, message);
string postData = GetPostStringFrom(postFieldNameValue);
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
request.ContentLength = byteArray.Length;
request.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + _authTokenString);
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
HttpStatusCode responseCode = ((HttpWebResponse)response).StatusCode;
if (responseCode.Equals(HttpStatusCode.Unauthorized) || responseCode.Equals(HttpStatusCode.Forbidden))
{
Console.WriteLine("Unauthorized - need new token");
}
else if (!responseCode.Equals(HttpStatusCode.OK))
{
Console.WriteLine("Response from web service not OK :");
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
}
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseLine = reader.ReadLine();
reader.Close();
response.Close();
return responseLine; // it never gets here, but an Exception is caught elsewhere
haha, it seems like I have to answer my own question again =)
So, I spent the last 48 hours testing, reading, searching for a solution to the HttepWebRequest problem, or rather the problem with HTTP requests.
All the suggestions I found was of no use to me, except this one:
http://www.eggheadcafe.com/community/vb/14/60113/webclient-to-http-post--error-on-receive.aspx
There is says that
After much research on the internet I stumbled upon suggestions that said to set Keep-Alive to FALSE and to use HTTP1.0.
but he also notes that it didnt work for him:
However, when I try to use that function, I am unable to successfully perform ANY POSTs on EITHER machine - my home or work PC
However, it seems to have solved my issue completely!
Lets hope it sticks =)
这篇关于HttpWebRequest和之后的GetResponse 1请求挂起;其他解决方案不适合我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!