我试图使用 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);
}
尝试了不同论坛的几种方法,但没有帮助。即使是具有上述代码的简单控制台应用程序也会无限期地挂起控制台!任何帮助都会很棒..
Thanks
你永远不会关闭StreamWriter
...所以我怀疑它没有被冲洗。诚然,我希望服务器会出现错误,而不仅仅是挂起,但它值得一看。
顺便说一句,您不需要关闭响应and将其丢弃。只是using
声明就足够了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)