C# HttpWebRequest未应答
我试图在WP7应用程序上使用HttpWebRequest,但我遇到了一个问题。我从未收到服务器的响应:/ 我做错了什么 这是错误的代码和平 Util类C# HttpWebRequest未应答,c#,windows-phone-7,httpwebrequest,C#,Windows Phone 7,Httpwebrequest,我试图在WP7应用程序上使用HttpWebRequest,但我遇到了一个问题。我从未收到服务器的响应:/ 我做错了什么 这是错误的代码和平 Util类 public class RequestState { // This class stores the State of the request. const int BUFFER_SIZE = 1024; public string requestData; public byte[] Data {
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();
}
你有没有试过在没有运气的情况下向几个不同的网站提出请求 我也有这个问题,那是因为我错过了请求的一些标题选项。
尝试为etc Mozilla下载一个插件,该插件可以嗅探您从浏览器到网站的请求,然后将请求中的标题添加到HttpWebRequest中的标题中。我一直都在做这种事情,但我从不阻止UI线程。我的建议是尝试在单独的线程上执行此操作,然后查看是否收到答复-可能是有什么东西试图进入UI线程(尽管不确定是什么),阻止了您完成请求。请提供一些代码,好吗?只需将代码保持原样并通过System.Threading.ThreadPool.QueueUserWorkItem()运行即可现在我得到一个异常“远程服务器返回一个错误:NotFound.”。我解决不了这个问题。我认为问题可能是windows phone emulator的internet连接…尝试在emulator上运行IE,看看是否可以看到web。