0

我需要创建一个带有两个参数的 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

4 回答 4

0

您不使用 .NET 网页,您必须创建 .NET Web 服务,因为只有 WebService 具有发布和获取方法。您不请求网页。网页没有收到您的消息和答复。

于 2013-02-18T13:29:35.753 回答
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;
}
于 2013-02-18T12:14:29.590 回答
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()));

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

于 2013-02-18T12:36:31.833 回答
0

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

于 2013-02-18T11:57:54.673 回答