HttpPost参数不适用于ASP服务器

时间:2013-02-18 11:40:29

标签: java android http-post httpclient

我需要创建一个带有两个参数的HttpPost请求。我同意有很多例子,但我相信我已经完成了我的研究,但我仍然没有得到ASP服务器的回复。我已经尝试过NameValuePair,但我似乎无法获得响应页面。我猜测参数没有被添加到httpPost对象。

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();

我在这里做错了什么?

4 个答案:

答案 0 :(得分:0)

我猜你在网址末尾错过了?

答案 1 :(得分:0)

尝试这样的事情:

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;
}

答案 2 :(得分:0)

当您使用浏览器(https://portal.sibt.nsw.edu.au/Default.asp)到达主页时,服务器会打开会话(您可以使用网络检查器查看set-cookie标头):

Set-Cookie:ASPSESSIONIDQCCCQQCQ=HJMMOCFAFILCLNCJIMNBACIB; path=/

在POST登录之前,您可能需要向此页面发出GET请求。此外,您必须在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()));

由于我没有有效的用户名/密码,我无法尝试此解决方案。

答案 3 :(得分:0)

您不使用.NET网页,您必须创建.NET Web服务,因为只有WebService具有post和get方法。您不需要网页。网页无法收到您的消息和答案。