[英]Login to mediawiki by api on wp7
I am trying to create an app for wp7 to login to wikipedia and help with translating the pages. 我正在尝试为wp7创建一个应用程序,以登录到Wikipedia并帮助翻译页面。 I am stuck right at the beginning since I can't get it to login through the mediawiki API. 我一开始就陷入困境,因为我无法通过mediawiki API登录。 The relevant part of the code goes like this: 代码的相关部分如下所示:
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();
}
The problems are: 问题是:
On the GetRequestStreamCallback I can't pass the data string from the main function. 在GetRequestStreamCallback上,我无法从主函数传递数据字符串。 How do I do this? 我该怎么做呢?
On the GetResponseCallback function how do I return the responsestring String so that I can output later? 在GetResponseCallback函数上,如何返回responsestring字符串,以便以后输出?
What you're actually trying to do is two asynchronous operations, one after another - it's worth remembering that the BeginXxx
methods return before they've finished - so in your case, you ask for the request stream to write to, and immediately ask for the response, so bad things will ensue. 您实际上要执行的操作是两个异步操作,一个接一个地执行-值得记住, BeginXxx
方法在完成之前会返回-因此,在您的情况下,您要求写入请求流,并立即请求响应,因此会发生坏事。
It's possibly worth looking at other examples, such as those in opensource code - you'll see that you don't typically call BeginGetResponse
until you've finished writing to the stream returned by EndGetRequestStream
可能值得看一下其他示例,例如开源代码中的示例-您会发现,在完成写入EndGetRequestStream
返回的流之前,通常不调用BeginGetResponse
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.