亚洲精品中文字幕无乱码_久久亚洲精品无码AV大片_最新国产免费Av网址_国产精品3级片

SUN認(rèn)證

JAVA實(shí)現(xiàn)httpClient的實(shí)例

時(shí)間:2024-11-05 11:03:45 SUN認(rèn)證 我要投稿
  • 相關(guān)推薦

關(guān)于JAVA實(shí)現(xiàn)httpClient的實(shí)例

  HttpClient 是 Apache Jakarta Common 下的子項(xiàng)目,可以用來(lái)提供高效的、最新的、功能豐富的支持 HTTP 協(xié)議的客戶端編程工具包,并且它支持 HTTP 協(xié)議最新的版本和建議。下面小編準(zhǔn)備了關(guān)于JAVA實(shí)現(xiàn)httpClient的實(shí)例,提供給大家參考!

  package net.xfok.http;

  import java.util.HashMap; import java.util.Iterator; import java.util.Map;

  import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod;

  /**

  */

  public class MyClient {

  /**

  *的到Http請(qǐng)求結(jié)果

  * @param url請(qǐng)求地址

  * @param parms請(qǐng)求參數(shù)

  * @return

  */

  public byte[] getBody(String url,Map parms){

  byte[] body=null;

  //構(gòu)造HttpClient的實(shí)例

  HttpClient httpClient = new HttpClient();

  //創(chuàng)建GET方法的實(shí)例

  PostMethod postMethod = new PostMethod(url);

  //填入各個(gè)表單域的值

  NameValuePair[] data = new NameValuePair[parms.keySet().size()]; Iterator it = parms.entrySet().iterator();

  int i=0;

  while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); Object key = entry.getKey(); Object value = entry.getValue(); data[i]=new NameValuePair(key.toString(),value.toString());

  i++;

  }

  //將表單的值放入postMethod中

  postMethod.setRequestBody(data);

  try {

  //執(zhí)行postMethod

  int statusCode = httpClient.executeMethod(postMethod); // HttpClient對(duì)于要求接受后繼服務(wù)的請(qǐng)求,象POST和PUT等不能自動(dòng)處理轉(zhuǎn)發(fā)

  // 301或者302

  if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {

  //從頭中取出轉(zhuǎn)向的地址

  Header locationHeader = postMethod

  .getResponseHeader(“location”);

  String location = null;

  if (locationHeader != null) { location = locationHeader.getValue(); System.out .println(“The page was redirected to:” + location);

  } else {

  System.err.println(“Location field value is null.”);

  }

  }

  body = postMethod.getResponseBody();

  } catch (Exception e) {

  e.printStackTrace();

  }

  return body;

  }

  public static void main(String[] args) {

  String url=“http://www.xfok.net”;

  Map parms=new HashMap();

  MyClient client=new MyClient();

  String result=new String(client.getBody(url, parms)); System.out.println(result);

  }

  }

【JAVA實(shí)現(xiàn)httpClient的實(shí)例】相關(guān)文章:

PHP實(shí)現(xiàn)RSA加密類的實(shí)例解析08-24

使用JavaScript實(shí)現(xiàn)Java的List功能08-09

Javascript簡(jiǎn)單實(shí)現(xiàn)面向?qū)ο缶幊汤^承實(shí)例代碼10-14

java實(shí)現(xiàn)web服務(wù)器的方法09-26

jQuery實(shí)現(xiàn)的拖動(dòng)調(diào)整表格單元格的大小代碼實(shí)例08-27

關(guān)于jQuery實(shí)現(xiàn)頁(yè)面頂部顯示的進(jìn)度條效果完整實(shí)例09-16

JavaScript實(shí)例講解08-29

Java與Java web的區(qū)別08-22

photoshop實(shí)例教程07-08

新托?谡Z(yǔ)實(shí)例09-29