C# 如何通过同一webclient(带有cookie)实例发出序列http请求 请考虑我的情况:

C# 如何通过同一webclient(带有cookie)实例发出序列http请求 请考虑我的情况:,c#,windows-phone-7,webclient,C#,Windows Phone 7,Webclient,我正在开发一个WindowsPhone7应用程序,它向我们学校的一个服务器发送HTTP POST请求,从中获取一些信息。当你访问网站时,它会显示一个验证码图像,你应该输入你的学校号码、密码以及验证码来登录。然后你可以得到你想要的任何东西 我有经验向comfirm确认,服务器将在cookie上写入客户端,以确保您已登录。但是我们知道,无论是windows phone还是windows phone中的类都只支持异步操作。如果我想实现登录过程,我必须在getVerifyCode()方法的uploadS

我正在开发一个WindowsPhone7应用程序,它向我们学校的一个服务器发送HTTP POST请求,从中获取一些信息。当你访问网站时,它会显示一个验证码图像,你应该输入你的学校号码、密码以及验证码来登录。然后你可以得到你想要的任何东西

我有经验向comfirm确认,服务器将在cookie上写入客户端,以确保您已登录。但是我们知道,无论是windows phone还是windows phone中的类都只支持异步操作。如果我想实现登录过程,我必须在getVerifyCode()方法的uploadStringCompleted方法中编写代码。我认为这不是最好的做法。例如:

(注意:这只是一个示例,不是真正的代码,因为要获取验证代码,我只需要一个get方法HTTP请求,我认为它可以很好地说明问题)

简而言之,我想知道解决上述问题的最佳实践或设计模式是什么


非常感谢。

以下是使用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();
}

请注意,您应该使用ThreadPool.QueueUserWorkItem执行它,以保持UI/主线程响应

旧线。但最后,您不可能阅读webClient.ResponseHeader<代码>字符串cookies=webClient.ResponseHeaders[“设置Cookie”]或者更确切地说是
((网络客户端)发送者)。ResponseHeader
这就是我要做的。不过,我在评论中考虑到,您也想检索cookies。
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();
}