Windows phone 7 httpWebRequest使用POST方法-接收意外的xml响应
我需要使用POST将字符串发布到服务器并获取xml响应,状态代码为OK,但字符串响应始终为=“”(0字节)。我的代码有什么问题吗?我从blackberry检查服务器,工作正常,因此问题一定来自我的代码:Windows phone 7 httpWebRequest使用POST方法-接收意外的xml响应,windows-phone-7,post,httpwebrequest,Windows Phone 7,Post,Httpwebrequest,我需要使用POST将字符串发布到服务器并获取xml响应,状态代码为OK,但字符串响应始终为=“”(0字节)。我的代码有什么问题吗?我从blackberry检查服务器,工作正常,因此问题一定来自我的代码: protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { s = NavigationContext.QueryString["para
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
s = NavigationContext.QueryString["parameter1"];
//string strConnectUrl = "http://www.contoso.com/example.aspx";
base.OnNavigatedTo(e);
// DoWebClient(s);
try
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(strConnectUrl);
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
// start the asynchronous operation
httpWebRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), httpWebRequest);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
// string XML_REQUEST = "<?xml version=\"1.0\"?><mybroker"><getConnections></mybroker>";
string post = "?&track=love";
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
Stream postStream = request.EndGetRequestStream(asynchronousResult);
// Convert the string into a byte array.
byte[] postBytes = Encoding.UTF8.GetBytes(post);
// Write to the request stream.
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Close();
// Start the asynchronous operation to get the response
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
static Stream str;
static string st;
private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
HttpStatusCode rcode = response.StatusCode;
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
//****THIS ALWAYS RETURN "" VALUE, EXPECT TO RETURN XML STRING****
string responseString = streamRead.ReadToEnd();
//Console.WriteLine(responseString);
// Close the stream object
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
}
受保护的覆盖无效OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
s=NavigationContext.QueryString[“parameter1”];
//字符串strConnectUrl=”http://www.contoso.com/example.aspx";
基地。导航到(e);
//道威客户;
尝试
{
HttpWebRequest HttpWebRequest=(HttpWebRequest)WebRequest.Create(strConnectUrl);
httpWebRequest.Method=“POST”;
httpWebRequest.ContentType=“application/x-www-form-urlencoded;charset=UTF-8”;
//启动异步操作
httpWebRequest.BeginGetRequestStream(新的异步回调(GetRequestStreamCallback),httpWebRequest);
}
捕获(例外情况除外)
{
Show(例如ToString());
}
}
私有静态void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
//字符串XML_请求="已使用Fiddler检查了您的呼叫。服务器返回了一个空的正文。您的代码工作正常。问题出在服务器端。Fiddler说请求很好,响应确实是空的。如果它是从Blackberry而不是从WP7工作的,服务器是否可以执行一些用户代理检查而不返回任何内容因为它不识别WP7用户代理而被拒绝
另外,看起来您正在发布一个查询字符串(“?&track=love”),这有点不寻常。您确定在请求中发送的数据正确吗?post值应为“track=love”
我试过了,它成功了,但响应是gzip编码的。可能值得检查rcode是否成功。