c# httpwebrequest getResponse() 冻结并挂起我的程序

发布于 2024-09-09 07:01:52 字数 1514 浏览 8 评论 0原文

我试图使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

·深蓝 2024-09-16 07:01:52

你永远不会关闭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.

可是我不能没有你 2024-09-16 07:01:52

如果远程服务器没有响应,除了定义一个超时并捕获异常,以通知用户操作无法完成,因为远程站点没有响应,您无能为力。响应:

var request = (HttpWebRequest)WebRequest.Create("https://qrua.com/qr/service/auth/login");
request.Timeout = 5000;
// If the server doesn't respond within 5 seconds you might catch the timeout exception
using (var response = request.GetResponse())
{

}

如果您不想冻结 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:

var request = (HttpWebRequest)WebRequest.Create("https://qrua.com/qr/service/auth/login");
request.Timeout = 5000;
// If the server doesn't respond within 5 seconds you might catch the timeout exception
using (var response = request.GetResponse())
{

}

If you don't want to freeze the UI you could use the async version: BeginGetResponse

别念他 2024-09-16 07:01:52

尝试指定 request.ContentLength。

在做之前:

        StreamWriter writer = new StreamWriter(request.GetRequestStream());
        writer.Write(credentialsJson);

尝试这样的事情:

using (MemoryStream stream = new MemoryStream())
{
    using (var writer = StreamWriter writer = new StreamWriter(stream))
    {
        writer.Write(credentialsJson);
        writer.Close();
    }
    request.ContentLength = stream.Length;
}

Try specifying request.ContentLength.

Before doing:

        StreamWriter writer = new StreamWriter(request.GetRequestStream());
        writer.Write(credentialsJson);

Try something like this:

using (MemoryStream stream = new MemoryStream())
{
    using (var writer = StreamWriter writer = new StreamWriter(stream))
    {
        writer.Write(credentialsJson);
        writer.Close();
    }
    request.ContentLength = stream.Length;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文