c# httpwebrequest getResponse() 冻结并挂起我的程序
我试图使用 httpwebrequest 在远程服务器上使用类似于休息的服务,从第一次执行本身开始,我的代码就挂起了程序。然后我尝试将它作为控制台应用程序,以确保它与程序本身无关,但没有运气!
string credentialsJson = @"{""username"":""test"",
""password"":""test""
}";
int tmp = ServicePointManager.DefaultConnectionLimit;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://qrua.com/qr/service" + @"/auth/login");
request.Method = "POST";
request.KeepAlive = true;
request.Timeout = 50000 ;
request.CookieContainer = new CookieContainer();
request.ContentType = "application/json";
try
{
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(credentialsJson);
}
catch (Exception e)
{
Console.WriteLine("EXCEPTION:" + e.Message);
}
//WebResponse response = request.GetResponse();
try
{
using (WebResponse response = (HttpWebResponse)request.GetResponse())
{
Console.WriteLine("request:\n" + request.ToString() + "\nresponse:\n" + response.ContentLength);
response.Close();
}
}
catch (Exception e)
{
Console.WriteLine("EXCEPTION: in sending http request:" + " \nError message:" + e.Message);
}
尝试了不同论坛的几种方法,但没有帮助。即使是具有上述代码的简单控制台应用程序也会无限期地挂起控制台!任何帮助都会很棒..
谢谢
I was trying to use httpwebrequest to use a rest like service on a remote server and from the first execution itself, my code was hanging the program. Then I tried it as a console application to make sure it has nothing to do with the program itself but no luck!
string credentialsJson = @"{""username"":""test"",
""password"":""test""
}";
int tmp = ServicePointManager.DefaultConnectionLimit;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://qrua.com/qr/service" + @"/auth/login");
request.Method = "POST";
request.KeepAlive = true;
request.Timeout = 50000 ;
request.CookieContainer = new CookieContainer();
request.ContentType = "application/json";
try
{
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(credentialsJson);
}
catch (Exception e)
{
Console.WriteLine("EXCEPTION:" + e.Message);
}
//WebResponse response = request.GetResponse();
try
{
using (WebResponse response = (HttpWebResponse)request.GetResponse())
{
Console.WriteLine("request:\n" + request.ToString() + "\nresponse:\n" + response.ContentLength);
response.Close();
}
}
catch (Exception e)
{
Console.WriteLine("EXCEPTION: in sending http request:" + " \nError message:" + e.Message);
}
Tried several things from different forums but it doesnt help. Even a simple console app with the above code hangs the console indefinitely! Any help would be great..
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你永远不会关闭
StreamWriter
...所以我怀疑它没有被刷新。诚然,我希望服务器会出现错误,而不仅仅是挂起,但它值得一看。顺便说一句,您不需要关闭响应并处置它。只需
using
语句就足够了。You're never closing the
StreamWriter
... so I suspect it's not being flushed. Admittedly I'd expect an error from the server instead of just a hang, but it's worth looking at.Btw, you don't need to close the response and dispose it. Just the
using
statement is enough.如果远程服务器没有响应,除了定义一个超时并捕获异常,以通知用户操作无法完成,因为远程站点没有响应,您无能为力。响应:
如果您不想冻结 UI,可以使用异步版本:BeginGetResponse
There's not much you can do if the remote server is not responding other than defining a
Timeout
and catch the exception as you did in order to inform the user that the operation cannot complete because the remote site didn't respond:If you don't want to freeze the UI you could use the async version: BeginGetResponse
尝试指定 request.ContentLength。
在做之前:
尝试这样的事情:
Try specifying request.ContentLength.
Before doing:
Try something like this: