Referer:http://jw.zhku.edu.cn/jwmis/ZNPK/KBFB_ClassSel.aspx
public void getRemoteInfo() {
// 定义待请求的URL
String requestUrl = "http://jw.zhku.edu.cn/jwmis/ZNPK/KBFB_ClassSel_rpt.aspx";
// 创建HttpClient实例
HttpClient client = new DefaultHttpClient();
// 根据URL创建HttpPost实例
HttpPost post = new HttpPost(requestUrl);
List<NameValuePair> params = new ArrayList<NameValuePair>();
// 设置需要传递的参数
params.add(new BasicNameValuePair("Sel_XNXQ", "20140"));
params.add(new BasicNameValuePair("txtxzbj", ""));
params.add(new BasicNameValuePair("Sel_XZBJ", "2012090701"));
params.add(new BasicNameValuePair("type", "1"));
try {
// 设置URL编码
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
// 发送请求并获取反馈
HttpResponse response = client.execute(post);
String result = null;
// 解析返回的内容
result = EntityUtils.toString(response.getEntity(),"utf-8");
// 将查询结果经过解析后显示在TextView中
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
返回是 :<html>
<head>
<title>未将对象引用设置到对象的实例。</title>
<style>
。。。
。。
。。
。。
------解决思路----------------------
试试下面这个方法,使用HTTPclient4.0.3及以上,还有不要传递空参数: params.add(new BasicNameValuePair("txtxzbj", ""));,这样做没意义。http://www.oschina.net/code/snippet_54371_1515
public static String post(String url, List<NameValuePair> params) {
String body = null;
try {
// Post请求
HttpPost httppost = new HttpPost(url);
// 设置参数
httppost.setEntity(new UrlEncodedFormEntity(params));
// 发送请求
HttpResponse httpresponse = hc.execute(httppost);
// 获取返回数据
HttpEntity entity = httpresponse.getEntity();
body = EntityUtils.toString(entity);
if (entity != null) {
entity.consumeContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return body;
}
------解决思路----------------------
这个问题是服务器后台处理的问题。
可能原因是后台没有对未登录用户做过滤,从而导致方法执行时未检查用户信息是否为NULL。
解决办法,给HttpClient加上CookieStore也就是Cookie存储功能,然后在请求之前先用你的用户名和密码登陆(为了服务器能返回标记用户登录信息的Cookie),然后再调用你现在的功能试试。