捕获 HTTP 响应时出现问题

发布于 2024-10-05 11:01:42 字数 920 浏览 5 评论 0原文

我有这段代码可以成功发出 HTTP 请求:

                //Successful request
                var requestInBytes = encoding.GetBytes(urlWithParameters.ToString());

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(urlWithParameters.ToString());
                req.Method = "POST";
                req.ContentLength = requestInBytes.Length;
                req.ContentType = "application/x-www-form-urlencoded";

                Stream newStream = req.GetRequestStream();

                // Send the data.
                newStream.Write(requestInBytes, 0, requestInBytes.Length);
                newStream.Close();

但是,我在捕获响应时遇到了问题。现在,我正在尝试这样做:

            //No response?
            System.IO.StreamReader st = new StreamReader(((HttpWebResponse)req.GetResponse()).GetResponseStream());
            var response = st.ReadLine();

但这会带来空白响应吗?

I have this code which successfully makes an HTTP request:

                //Successful request
                var requestInBytes = encoding.GetBytes(urlWithParameters.ToString());

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(urlWithParameters.ToString());
                req.Method = "POST";
                req.ContentLength = requestInBytes.Length;
                req.ContentType = "application/x-www-form-urlencoded";

                Stream newStream = req.GetRequestStream();

                // Send the data.
                newStream.Write(requestInBytes, 0, requestInBytes.Length);
                newStream.Close();

However, I'm having trouble capturing the response. Right now, I'm trying this:

            //No response?
            System.IO.StreamReader st = new StreamReader(((HttpWebResponse)req.GetResponse()).GetResponseStream());
            var response = st.ReadLine();

But this is coming back with a blank response?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

软甜啾 2024-10-12 11:01:42

尝试:

HTTPWebResponse response = req.GetResponse();

然后您可以检查响应的各种属性。

或者

尝试:

var response= req.ReadToEnd();

Try:

HTTPWebResponse response = req.GetResponse();

You can then check the various properties of the response.

OR

Try:

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