BufferedReader in = null;
try{
//【Step 1】创建一个HttpClient的对象(或使用已有的)
HttpClient client = new DefaultHttpClient();
//【Step 2】实例化一个HTTP GET或者HTTP POST,本例是HTTP POST
HttpPost request = new HttpPost("http://epub.cnki.net/kns/brief/default_result.aspx");
//【Step 3】设置HTTP参数,本例根据抓包的内容填写,这是体力活,在完整HTTP服务的笔记后,会提供小例子下载。对于HTTP Post,需要传递键值对信息,从上面的转包可以看到,这部分不是作为request URI,而是作为HTML Form URL Encoded,为此我们需要用户元素为NameValuePair格式的list来存储这些信息,并封装在UrlEncodedFormEntiry对象中。通过setEntity()加入到请求对象中。 List postParameters = new ArrayList();
postParameters.add(new BasicNameValuePair("txt_1_sel","TI$%=|"));
postParameters.add(new BasicNameValuePair("txt_1_value1","Android"));
… …
postParameters.add(new BasicNameValuePair("ua","1.11"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
//【Step 4】通过HttpClient来执行HTTP call(发出HTTP请求)
HttpResponse response =client.execute(request);
//【Step 5】处理HTTP响应,本例将整个响应的内容(HTTP 200消息的body)都在String中。in = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()));
StringBuffer buff = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while((line = in.readLine())!= null){
buff.append(line + NL);
}
showInfo(buff.toString());
}catch(Exception e){
e.printStackTrace();
showInfo(e.toString());
}finally{
if(in != null){
try{
showInfo("== process in.colse() ==");
in.close();
}catch(Exception e){
e.printStackTrace();
showInfo(e.toString());
}
}
}