我试图在WP7应用程序上使用HttpWebRequest,但我遇到了问题。我从未收到服务器的回复:/
我做错了什么?
这是错误的代码和平...
Util Class
public class RequestState
{
// This class stores the State of the request.
const int BUFFER_SIZE = 1024;
public string requestData;
public byte[] Data
{
get
{
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] encodedPostData = ascii.GetBytes(this.requestData);
return encodedPostData;
}
}
public byte[] BufferRead;
public HttpWebRequest request;
public HttpWebResponse response;
public RequestState()
{
BufferRead = new byte[BUFFER_SIZE];
requestData = string.Empty;
request = null;
}
}
方法......
private static ManualResetEvent allDone = new ManualResetEvent(false);
private static string PostRequest(string service, string email, string password, string source)
{
// Prepare request.
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(clientLoginUrl);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
// Create an instance of the RequestState and assign the previous myHttpWebRequest1
// object to it's request field.
RequestState myRequestState = new RequestState();
myRequestState.request = request;
myRequestState.requestData = String.Format(postData, service, email, password, source);
// Get the response that will contain the Auth token.
try
{
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequestState);
}
catch (WebException ex)
{
HttpWebResponse faultResponse = ex.Response as HttpWebResponse;
if (faultResponse != null && faultResponse.StatusCode == HttpStatusCode.Forbidden)
throw new IncorrectUsernameOrPasswordException(faultResponse.StatusCode, faultResponse.StatusDescription);
else
throw;
}
// Keep the main thread from continuing while the asynchronous
allDone.WaitOne();
if (myRequestState.response != null)
{
// Check for login failed.
if (myRequestState.response.StatusCode != HttpStatusCode.OK)
throw new LoginFailedException(myRequestState.response.StatusCode, myRequestState.response.StatusDescription);
// Read.
using (StreamReader reader = new StreamReader(myRequestState.response.GetResponseStream()))
return reader.ReadToEnd();
}
else
return string.Empty;
}
private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
RequestState requestState = (RequestState)asynchronousResult.AsyncState;
HttpWebRequest request = (HttpWebRequest)requestState.request;
// End the operation
Stream postStream = request.EndGetRequestStream(asynchronousResult);
// Write to the request stream.
postStream.Write(requestState.Data, 0, requestState.requestData.Length);
postStream.Close();
// Start the asynchronous operation to get the response
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), requestState);
}
private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
RequestState requestState = (RequestState)asynchronousResult.AsyncState;
HttpWebRequest request = (HttpWebRequest)requestState.request;
// End the operation
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
requestState.response = response;
allDone.Set();
}
答案 0 :(得分:0)
你试过没有运气的几个不同网站的请求吗?
我也有这个问题,那是因为我错过了请求的一些标题选项。 尝试下载Mozilla的插件,它可以从浏览器中嗅探您的请求到网站,然后将请求中的那些标题添加到HttpWebRequest中的标题中。