HttpPost参数无法工作到ASP服务器
我需要创建一个带有两个参数的HttpPost请求。我同意有很多例子,但我相信我已经完成了我的研究,但我仍然没有从ASP服务器获得回应。我试过NameValuePair,但我似乎无法获得响应页面。我猜测参数没有被添加到httpPost对象。HttpPost参数无法工作到ASP服务器
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://portal.sibt.nsw.edu.au/Default.asp");
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("userID", id.getText().toString()));
nameValuePair.add(new BasicNameValuePair("password", pword.getText().toString()));
post.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpResponse response = client.execute(post);
String responseContent = EntityUtils.toString(response.getEntity());
Log.d("response to string", responseContent);
我又收到登录页面,这个代码返回一个NullPointerException:
String newURL = response.getFirstHeader("Location").getValue();
我在做什么错在这里?
我猜你只是错过?
在您的网址的结尾
尝试这样的事:
private static String urlAppendParams(String url, List<NameValuePair> lNameValuePairs) {
if (lNameValuePairs != null) {
List<NameValuePair> llParams = new LinkedList<NameValuePair>();
for (NameValuePair nameValuePair : lNameValuePairs) {
llParams.add(nameValuePair);
}
String sParams = URLEncodedUtils.format(llParams, "UTF-8");
if (!url.endsWith("?")) {
url += "?";
}
url += sParams;
}
return url;
}
当你使用浏览器(https://portal.sibt.nsw.edu.au/Default.asp)主页到达时,服务器将打开会话(你可以看到设置cookie头与网络检查员):
Set-Cookie:ASPSESSIONIDQCCCQQCQ=HJMMOCFAFILCLNCJIMNBACIB; path=/
也许你需要做一个GET请求,此页面在POST登录之前。另外,您必须在HttpClient中启用cookie跟踪。我通常使用:
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
final HttpGet get = new HttpGet("https://portal.sibt.nsw.edu.au/Default.asp");
client.execute(get); //perform an initial GET to receive the session cookie
//now do the post...
HttpPost post = new HttpPost("https://portal.sibt.nsw.edu.au/Default.asp?Summit=OK");
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("hcUserID", id.getText().toString()));
nameValuePair.add(new BasicNameValuePair("hcPassword", pword.getText().toString()));
post.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpResponse response = client.execute(post);
//I should be logged in.... Lets get the an internal webpage
get = new HttpGet("https://portal.sibt.nsw.edu.au/std_Alert.asp");
client.execute(get);
System.out.println(new String(get.getResponseBody()));
由于我没有有效的用户/密码,我无法尝试此解决方案。
我似乎无法找到setCookiePolicy()方法?也不是executeMethod()我只能找到的execute() 你可以尝试使用该用户名和密码 用户名chsac1203 密码1234567k 请及时通知我尽快 – Sabbib 2013-02-20 12:18:48
嗯,也许我在一个旧的HttpClient API是说,我将更新代码与新的API – lipido 2013-02-20 19:43:46
我也尝试过使用我的浏览器进行登录/传递,并得到一条消息:“对不起,您的用户ID或密码不正确;或者您的门户帐户被禁用。” – lipido 2013-02-20 19:54:06
您不使用.NET网页,您必须创建.NET Web服务,因为只有WebService具有发布和获取方法。你不要求网页。网页没有收到您的信息和答案。
这不是我的网页,它是我的大学,还有什么其他的选择我有? – Sabbib 2013-02-20 12:16:53
当你成功登录时,你在URL文本中看到了什么,如果你加密的文本不能做任何事情,因为在你需要“网络服务”之前我会解释它。 – nurisezgin 2013-02-21 18:28:07
请提交的logcat期待着更多的帮助 – 2013-02-18 11:46:00