开发者

HttpWebRequest/HttpWebResponse firing two calls to the server

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开发者_如何学Python...
        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);
        }


try this~

httpWebRequest.ServicePoint.ConnectionLimit = 10;


Appears this is a bug in the Microsoft testing design.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜