简体   繁体   English

如何通过相同的Web客户端(带有Cookie)实例发出序列HTTP请求

[英]How to make sequence http request by the same webclient (with cookie) instance

Please consider my situation: 请考虑我的情况:

I am developing an windows phone 7 application which send HTTP POST request to one of server in our school to get some information from it. 我正在开发Windows Phone 7应用程序,该应用程序将HTTP POST请求发送到我们学校中的一台服务器,以从中获取一些信息。 When you access to the website, it show you an verify code image, you should input your school number, password along with the verify code to login it. 当您访问该网站时,它会显示一个验证码图像,您应该输入您的学校编号,密码以及验证码才能登录。 Then you can get access to whatever you want. 然后,您可以访问所需的任何内容。

I have make experiences to comfirm that, the server will write on cookie to client to make sure that you are logged in. But we know that no matter the WebClient or the HttpWebRequest class in windows phone are both only support async operation. 我有经验可以确认,服务器将在cookie上写入客户端以确保您已登录。但是我们知道,无论Windows Phone中的WebClient还是HttpWebRequest类都仅支持异步操作。 If I want implement the log in process, I must write code in getVerifyCode() method's uploadStringCompleted method. 如果要实现登录过程,则必须在getVerifyCode()方法的uploadStringCompleted方法中编写代码。 I think it's not the best practice. 我认为这不是最佳做法。 For example: 例如:

(NOTICE:this is just an example, not the real code, because to get the verify code I just need a GET method HTTP request, I think it can illustrate the problem confuse me well) (注意:这只是一个示例,不是真实的代码,因为要获取验证代码,我只需要一个GET方法HTTP请求,我认为它可以说明问题,这使我感到困惑)

public void getVerifyCode()
{
    webClient.uploadStringCompleted += new uploadStringCompleted(getVerifyCodeCompleted);
    webClient.uploadStringAsync(balabala, balabala, balabala);
}

private void getVerifyCodeCompleted(object sender, uploadStringCompletedArgs e)
{
    if(e.Error == null)
    {
        webClient.uploadStringCompleted -= getVerifyCodeCompleted;

        // start log in 
        // I don't submit a new request inside last request's completed event handler
        // but I can't find a more elegent way to do this.
        webClient.uploadStringCompleted += loginCompleted;
        webClient.uploadStringAsync(balabala, balabala, balabala);
    }
}

So in a nutshell, I wanna to know what is the best practice or design pattern to solve the problem such as above? 简而言之,我想知道解决上述问题的最佳实践或设计模式是什么?

Thanks a lot in advance. 非常感谢。

Here is a code snippet using HttpWebRequest.BeginGetRequestStream / EndRequestStream : 这是使用HttpWebRequest.BeginGetRequestStream / EndRequestStream的代码片段:

HttpWebRequest webRequest = WebRequest.Create(@"https://www.somedomain.com/etc") as HttpWebRequest;
webRequest.ContentType = @"application/x-www-form-urlencoded";
webRequest.Method = "POST";

// Prepare the post data into a byte array
string formValues = string.Format(@"login={0}&password={1}", "someLogin", "somePassword");
byte[] byteArray = Encoding.UTF8.GetBytes(formValues);

// Set the "content-length" header 
webRequest.Headers["Content-Length"] = byteArray.Length.ToString();

// Write POST data
IAsyncResult ar = webRequest.BeginGetRequestStream((ac) => { }, null);
using (Stream requestStream = webRequest.EndGetRequestStream(ar) as Stream)
{
    requestStream.Write(byteArray, 0, byteArray.Length);
    requestStream.Close();
}

// Retrieve the response
string responseContent;    

ar = webRequest.BeginGetResponse((ac) => { }, null);
WebResponse webResponse = webRequest.EndGetResponse(ar) as HttpWebResponse;
try
{
    // do something with the response ...
    using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
    {
        responseContent = sr.ReadToEnd();
        sr.Close();
    }
}
finally
{
    webResponse.Close();
}

Please note that you should execute it with a ThreadPool.QueueUserWorkItem in order to keep the UI/main thread responsive. 请注意,您应该使用ThreadPool.QueueUserWorkItem执行它,以使UI /主线程保持响应状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM