我在Windows Phone 8 / 8.1中开发了一个应用程序,现在我正在尝试为Android用户开发。我从网站获取数据并显示在我的应用程序中。网站需要登录用户名,密码。我在C#中成功实现了它。但我无法在java中做到这一点。有些人可以指导我。
我在C#中的代码:
HttpClientHandler handler = new HttpClientHandler();
HttpClient client = new HttpClient(handler, false);
handler.AllowAutoRedirect = true;
handler.PreAuthenticate = true;
Uri vceURI = new Uri("http://xxxx/default.aspx");
var html = await client.GetStringAsync(siteURI);
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var viewstateNode = doc.GetElementbyId("__VIEWSTATE");
var eventvalidationNode = doc.GetElementbyId("__EVENTVALIDATION");
var viewstate = viewstateNode.GetAttributeValue("value", "");
var eventvalidation = eventvalidationNode.GetAttributeValue("value", "");
var formContent1 = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("__VIEWSTATE", viewstate),
new KeyValuePair<string, string>("__EVENTVALIDATION", eventvalidation),
new KeyValuePair<string,string>("txtLoginID",_username),
new KeyValuePair<string,string>("txtPWD",_pswd),
new KeyValuePair<string,string>("btnLogin","Go%21")
});
var outputDoc = await client.PostAsync("http://xxx/default.aspx", formContent1);
html = await outputDoc.Content.ReadAsStringAsync();
doc.LoadHtml(html);
// parsing(doc);
var studentInfoNode = doc.GetElementbyId("divStudInfo");
var _studentinfonode1 = studentInfoNode.ChildNodes;
var studentInfo = studentInfoNode.ChildNodes[0].ChildNodes;
上述studentInfo对象包含所需数据
我尝试使用HttpClient + HttpPost,但我最终在null异常:(
如果有人曾经在类似的应用程序上工作过,请帮助我。
由于