HttpWebRequest/HttpWebResponse 向服务器发起两次调用
下面是我用来将数据发布到基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个~
try this~
看来这是微软测试设计中的一个错误。
Appears this is a bug in the Microsoft testing design.