我正在尝试为 wp7 创建一个应用程序以登录维基百科并帮助翻译页面。我一开始就被卡住了,因为我无法通过 mediawiki API 登录。代码的相关部分如下所示:
data.Append("action=login&lgname" + HttpUtility.UrlEncode(textBox1.Text));
data.Append("&lgpassword=" + HttpUtility.UrlEncode(passwordBox1.Password));
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback),request);
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
public void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asynchronousResult);
byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
postStream.Write(byteData, 0, data.Length);
postStream.Close();
}
private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
streamResponse.Close();
streamRead.Close();
response.Close();
}
问题是:
在 GetRequestStreamCallback 上,我无法从主函数传递数据字符串。我该怎么做呢?
在 GetResponseCallback 函数上,如何返回响应字符串以便稍后输出?