我尝试使用HttpWebRequest在远程服务器上使用rest服务,但从第一次执行开始,我的代码就让程序卡住了。然后我将其尝试作为控制台应用程序运行,以确保它与程序本身无关,但没有成功!
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);
}
我尝试了不同论坛上的几种方法,但都没有帮助。即使是一个简单的控制台应用程序,使用上述代码也会无限期地挂起控制台!任何帮助将是非常感激的。
谢谢