HttpWebRequest/HttpWebResponse 向服务器发起两次调用

发布于 2024-11-30 08:38:48 字数 2026 浏览 0 评论 0原文

下面是我用来将数据发布到基于 REST 的服务器的代码片段。如果我开始在服务器上调试,我会看到两个单独的请求通过。我想知道的是为什么?

由于第二次通话,我的请求失败了。如果我调试,它显示第一个请求进展顺利,但是当我尝试读取客户端的响应时,它会向服务器发送新的调用,并且该请求无法通过身份验证。

下面内联的评论是对服务器的 2 个调用触发的地方......

        string requestUri = "/Service/Contacts";

        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(SERVICEBASEURI + requestUri);
        httpWebRequest.Headers.Add(AUTHENTICATE, m_AuthenticationKey);
        httpWebRequest.Headers.Add(UTCTIMESTAMP, m_UtcTime.ToString("yyyy'-'MM'-'dd HH':'mm':'ss'Z'"));
        httpWebRequest.Headers.Add(NONCE, m_NonceValue);
        httpWebRequest.Accept = "*/*";
        httpWebRequest.UserAgent = "Test-Framework";

        httpWebRequest.Method = "POST";

        string postData = "instance={\"FullName\":\"Altonymous\"}";
        byte[] postDataBytes = new ASCIIEncoding().GetBytes(postData);

        // TODO: Add postData to the Payload.  Needs to be done on authorization side as well.
        string requestPayload = GetPayload(requestUri);
        httpWebRequest.Headers.Add(AUTHORIZATION, requestPayload);
        httpWebRequest.ContentType = "application/x-www-form-urlencoded";
        httpWebRequest.ContentLength = postDataBytes.Length;

        Stream stream = httpWebRequest.GetRequestStream();
        // First call fires off to the server.  I didn't expect it to happen here...
        stream.Write(postDataBytes, 0, postDataBytes.Length);
        stream.Close();

        // Second call fires off to the server.  This is where I expected it to happen.
        using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
        {
            Assert.AreEqual(HttpStatusCode.OK, httpWebResponse.StatusCode);

            Stream responseStream = httpWebResponse.GetResponseStream();
            StreamReader responseStreamReader = new StreamReader(responseStream);
            string resultString = responseStreamReader.ReadToEnd();
            Assert.IsNotNullOrEmpty(resultString);
        }

Below is a code snippet I am using to post data to a REST based server. If I start debugging on the server I am seeing two separate requests come through. What I want to know is why?

My request is failing because of the second call. If I debug it shows the first request is going through fine, but when I try and read the response on the client side it sends over new call to the server and that one fails authentication.

Comments inline below where the 2 calls to the server fire off...

        string requestUri = "/Service/Contacts";

        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(SERVICEBASEURI + requestUri);
        httpWebRequest.Headers.Add(AUTHENTICATE, m_AuthenticationKey);
        httpWebRequest.Headers.Add(UTCTIMESTAMP, m_UtcTime.ToString("yyyy'-'MM'-'dd HH':'mm':'ss'Z'"));
        httpWebRequest.Headers.Add(NONCE, m_NonceValue);
        httpWebRequest.Accept = "*/*";
        httpWebRequest.UserAgent = "Test-Framework";

        httpWebRequest.Method = "POST";

        string postData = "instance={\"FullName\":\"Altonymous\"}";
        byte[] postDataBytes = new ASCIIEncoding().GetBytes(postData);

        // TODO: Add postData to the Payload.  Needs to be done on authorization side as well.
        string requestPayload = GetPayload(requestUri);
        httpWebRequest.Headers.Add(AUTHORIZATION, requestPayload);
        httpWebRequest.ContentType = "application/x-www-form-urlencoded";
        httpWebRequest.ContentLength = postDataBytes.Length;

        Stream stream = httpWebRequest.GetRequestStream();
        // First call fires off to the server.  I didn't expect it to happen here...
        stream.Write(postDataBytes, 0, postDataBytes.Length);
        stream.Close();

        // Second call fires off to the server.  This is where I expected it to happen.
        using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
        {
            Assert.AreEqual(HttpStatusCode.OK, httpWebResponse.StatusCode);

            Stream responseStream = httpWebResponse.GetResponseStream();
            StreamReader responseStreamReader = new StreamReader(responseStream);
            string resultString = responseStreamReader.ReadToEnd();
            Assert.IsNotNullOrEmpty(resultString);
        }

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

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

发布评论

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

评论(2

昔日梦未散 2024-12-07 08:38:48

试试这个~

httpWebRequest.ServicePoint.ConnectionLimit = 10;

try this~

httpWebRequest.ServicePoint.ConnectionLimit = 10;
傲影 2024-12-07 08:38:48

看来这是微软测试设计中的一个错误。

Appears this is a bug in the Microsoft testing design.

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