奶头挺立呻吟高潮av全片,成人试看120秒体验区,性欧美极品v,A片高潮抽搐揉捏奶头视频

java語言

java實(shí)現(xiàn)http請(qǐng)求工具類示例

時(shí)間:2024-09-09 02:49:19 java語言 我要投稿
  • 相關(guān)推薦

關(guān)于java實(shí)現(xiàn)http請(qǐng)求工具類示例

  通過http rest請(qǐng)求返回?cái)?shù)據(jù)

  復(fù)制代碼 代碼如下:

  import org.apache.commons.logging.Log;

  import org.apache.commons.logging.LogFactory;

  import org.apache.http.NameValuePair;

  import org.apache.http.client.ClientProtocolException;

  import org.apache.http.client.HttpClient;

  import org.apache.http.client.ResponseHandler;

  import org.apache.http.client.entity.UrlEncodedFormEntity;

  import org.apache.http.client.methods.HttpGet;

  import org.apache.http.client.methods.HttpPost;

  import org.apache.http.impl.client.BasicResponseHandler;

  import org.apache.http.impl.client.DefaultHttpClient;

  import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;

  import java.io.IOException;

  import java.text.MessageFormat;

  import java.util.List;

  import java.util.concurrent.TimeUnit;

  /**

  * 分裝一個(gè)http請(qǐng)求的工具類

  *

  * @author 顧煒【guwei】 on 14-4-22.下午3:17

  */

  public class HttpClientUtils {

  private static final Log log = LogFactory.getLog(HttpClientUtils.class);

  /**

  * 初始化HttpClient

  */

  private static HttpClient httpClient = null;

  /**

  * 生產(chǎn)HttpClient實(shí)例

  * 公開,靜態(tài)的工廠方法,需要使用時(shí)才去創(chuàng)建該單體

  *

  * @return

  */

  public static HttpClient getHttpClient() {

  if (httpClient == null) {

  httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());

  }

  return httpClient;

  }

  /**

  * POST方式調(diào)用

  *

  * @param url

  * @param params 參數(shù)為NameValuePair鍵值對(duì)對(duì)象

  * @return 響應(yīng)字符串

  * @throws java.io.UnsupportedEncodingException

  */

  public static String executeByPOST(String url, Listparams) {

  HttpClient httpclient = getHttpClient();

  HttpPost post = new HttpPost(url);

  ResponseHandlerresponseHandler = new BasicResponseHandler();

  String responseJson = null;

  try {

  if (params != null) {

  post.setEntity(new UrlEncodedFormEntity(params));

  }

  responseJson = httpclient.execute(post, responseHandler);

  log.info("HttpClient POST請(qǐng)求結(jié)果:" + responseJson);

  } catch (ClientProtocolException e) {

  e.printStackTrace();

  log.info("HttpClient POST請(qǐng)求異常:" + e.getMessage());

  } catch (IOException e) {

  e.printStackTrace();

  } finally {

  httpclient.getConnectionManager().closeExpiredConnections();

  httpclient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);

  }

  return responseJson;

  }

  /**

  * Get方式請(qǐng)求

  *

  * @param url 帶參數(shù)占位符的URL,例:http://****/User/user/center.aspx?_action=GetSimpleUserInfo&codes={0}&email={1}

  * @param params 參數(shù)值數(shù)組,需要與url中占位符順序?qū)?yīng)

  * @return 響應(yīng)字符串

  * @throws java.io.UnsupportedEncodingException

  */

  public static String executeByGET(String url, Object[] params) {

  HttpClient httpclient = getHttpClient();

  String messages = MessageFormat.format(url, params);

  HttpGet get = new HttpGet(messages);

  ResponseHandlerresponseHandler = new BasicResponseHandler();

  String responseJson = null;

  try {

  responseJson = httpclient.execute(get, responseHandler);

  log.info("HttpClient GET請(qǐng)求結(jié)果:" + responseJson);

  } catch (ClientProtocolException e) {

  e.printStackTrace();

  log.info("HttpClient GET請(qǐng)求異常:" + e.getMessage());

  } catch (IOException e) {

  e.printStackTrace();

  log.info("HttpClient GET請(qǐng)求異常:" + e.getMessage());

  } finally {

  httpclient.getConnectionManager().closeExpiredConnections();

  httpclient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);

  }

  return responseJson;

  }

  /**

  * @param url

  * @return

  */

  public static String executeByGET(String url) {

  HttpClient httpclient = getHttpClient();

  HttpGet get = new HttpGet(url);

  ResponseHandlerresponseHandler = new BasicResponseHandler();

  String responseJson = null;

  try {

  responseJson = httpclient.execute(get, responseHandler);

  log.info("HttpClient GET請(qǐng)求結(jié)果:" + responseJson);

  } catch (ClientProtocolException e) {

  e.printStackTrace();

  log.info("HttpClient GET請(qǐng)求異常:" + e.getMessage());

  } catch (IOException e) {

  e.printStackTrace();

  log.info("HttpClient GET請(qǐng)求異常:" + e.getMessage());

  } finally {

  httpclient.getConnectionManager().closeExpiredConnections();

  httpclient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);

  }

  return responseJson;

  }

  }

【java實(shí)現(xiàn)http請(qǐng)求工具類示例】相關(guān)文章:

java構(gòu)造函數(shù)實(shí)現(xiàn)代碼示例04-03

java system類使用方法示例04-03

Java編程學(xué)習(xí)示例04-03

關(guān)于循環(huán)的java代碼示例04-03

關(guān)于Java動(dòng)態(tài)實(shí)現(xiàn)的方法04-02

Java程序設(shè)計(jì)示例教程04-02

實(shí)現(xiàn)java屏幕抓屏的方法03-27

Java類的基本構(gòu)成11-18

什么是java集合類03-27

主站蜘蛛池模板: 嘉善县| 囊谦县| 望奎县| 新和县| 梅河口市| 惠州市| 呼图壁县| 陆川县| 平武县| 鸡泽县| 鞍山市| 太仆寺旗| 武义县| 如东县| 桦南县| 凌海市| 连南| 玛多县| 湟中县| 华坪县| 天全县| 乐业县| 通州市| 阿荣旗| 屏东县| 建水县| 中方县| 厦门市| 遂平县| 综艺| 岳普湖县| 广汉市| 瑞昌市| 肥乡县| 双流县| 岳阳县| 桐乡市| 天全县| 兴安盟| 开化县| 永川市|