HttpWebRequest not answering

本文关键字:answering not HttpWebRequest | 更新日期: 2023-09-27 18:29:15

我试图在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
    {
        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();
    }

HttpWebRequest not answering

你有没有尝试过向几个不同的网站请求,但没有成功?

我也有这个问题,那是因为我错过了请求的一些头球选项。试着下载一个etc-Mozilla插件,它可以将您的请求从浏览器嗅探到网站,然后将这些请求中的头添加到HttpWebRequest中的头中。