激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java實(shí)現(xiàn)Http工具類的封裝操作示例

Java實(shí)現(xiàn)Http工具類的封裝操作示例

2021-03-16 10:56記憶沉思 Java教程

這篇文章主要介紹了Java實(shí)現(xiàn)Http工具類的封裝操作,涉及java針對http請求與響應(yīng)、遠(yuǎn)程交互與字符串拼接等操作封裝技巧,需要的朋友可以參考下

本文實(shí)例講述了Java實(shí)現(xiàn)Http工具類的封裝操作。分享給大家供大家參考,具體如下:

http工具類的實(shí)現(xiàn):(通過apache包)第一個類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import com.gooagoo.stcu.utils.http.HttpClientUtils;
public class HTTPRequest {
  private String errorMessage; // 錯誤信息
  /**
   * HTTP請求字符串資源
   *
   * @param url
   *      URL地址
   * @return 字符串資源
   * */
  public String httpRequestString(String url) {
    String result = null;
    try {
      HttpEntity httpEntity = httpRequest(url);
      if (httpEntity != null) {
        result = EntityUtils.toString(httpEntity, "urf-8"); // 使用UTF-8編碼
      }
    } catch (IOException e) {
      errorMessage = e.getMessage();
    }
    return result;
  }
  /**
   * HTTP請求字節(jié)數(shù)組資源
   *
   * @param url
   *      URL地址
   * @return 字節(jié)數(shù)組資源
   * */
  public byte[] httpRequestByteArray(String url) {
    byte[] result = null;
    try {
      HttpEntity httpEntity = httpRequest(url);
      if (httpEntity != null) {
        result = EntityUtils.toByteArray(httpEntity);
      }
    } catch (IOException e) {
      errorMessage = e.getMessage();
    }
    return result;
  }
  /**
   * 使用HTTP GET方式請求
   *
   * @param url
   *      URL地址
   * @return HttpEntiry對象
   * */
  private HttpEntity httpRequest(String url) {
    HttpEntity result = null;
    try {
      HttpGet httpGet = new HttpGet(url);
      HttpClient httpClient = HttpClientUtils.getHttpClient();
      HttpResponse httpResponse;
      httpResponse = httpClient.execute(httpGet);
      int httpStatusCode = httpResponse.getStatusLine().getStatusCode();
      /*
       * 判斷HTTP狀態(tài)碼是否為200
       */
      if (httpStatusCode == HttpStatus.SC_OK) {
        result = httpResponse.getEntity();
      } else {
        errorMessage = "HTTP: " + httpStatusCode;
      }
    } catch (ClientProtocolException e) {
      errorMessage = e.getMessage();
    } catch (IOException e) {
      errorMessage = e.getMessage();
    }
    return result;
  }
  /**
   * 返回錯誤消息
   *
   * @return 錯誤信息
   * */
  public String getErrorMessage() {
    return this.errorMessage;
  }
}

第二個類的實(shí)現(xiàn):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.demo.http;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class HttpClientUtils {
  private static final int REQUEST_TIMEOUT = 5 * 1000;// 設(shè)置請求超時10秒鐘
  private static final int SO_TIMEOUT = 10 * 1000; // 設(shè)置等待數(shù)據(jù)超時時間10秒鐘
  // static ParseXml parseXML = new ParseXml();
  // 初始化HttpClient,并設(shè)置超時
  public static HttpClient getHttpClient() {
    BasicHttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
    HttpClient client = new DefaultHttpClient(httpParams);
    return client;
  }
  public static boolean doPost(String url) throws Exception {
    HttpClient client = getHttpClient();
    HttpPost httppost = new HttpPost(url);
    HttpResponse response;
    response = client.execute(httppost);
    if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
      return true;
    }
    client.getConnectionManager().shutdown();
    return false;
  }
  /**
   * 與遠(yuǎn)程交互的返回值post方式
   *
   * @param hashMap
   * @param url
   * @return
   */
  public static String getHttpXml(HashMap<String, String> hashMap, String url) {
    String responseMsg = "";
    HttpPost request = new HttpPost(url);
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    Iterator<Map.Entry<String, String>> iter = hashMap.entrySet()
        .iterator();
    while (iter.hasNext()) {
      Entry<String, String> entry = iter.next();
      params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    }
    try {
      request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
      HttpClient client = HttpClientUtils.getHttpClient();
      HttpResponse response = client.execute(request);
      if (response.getStatusLine().getStatusCode() == 200) {
        responseMsg = EntityUtils.toString(response.getEntity());
      }
    } catch (UnknownHostException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return responseMsg;
  }
  /**
   * map轉(zhuǎn)字符串 拼接參數(shù)
   *
   * @param hashMap
   * @return
   */
  public static String mapToString(HashMap<String, String> hashMap) {
    String parameStr = "";
    Iterator<Map.Entry<String, String>> iter = hashMap.entrySet()
        .iterator();
    while (iter.hasNext()) {
      Entry<String, String> entry = iter.next();
      parameStr += "&" + entry.getKey() + "=" + entry.getValue();
    }
    if (parameStr.contains("&")) {
      parameStr = parameStr.replaceFirst("&", "?");
    }
    return parameStr;
  }
}

希望本文所述對大家java程序設(shè)計有所幫助。

原文鏈接:http://blog.csdn.net/u012083681/article/details/17512271

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: xxx18hd18hd日本| 黄色大片在线观看 | 麻豆19禁国产青草精品 | 久久精品中文字幕一区二区三区 | 91精品国产乱码久久久久久久久 | h视频在线播放 | 久久久久久久久久久久网站 | 欧美黑人伦理 | 日韩蜜桃视频 | 91av在线免费播放 | 999精品久久久 | 国产精品视频一区二区三区四区五区 | 国产精品久久久久av | 国产精品欧美久久久久一区二区 | 视频精品二区 | 被玩坏了的女老师(高h np) | 国产91丝袜在线播放 | 国产日韩免费观看 | 一区二区精品视频在线观看 | 亚洲日韩中文字幕一区 | 国产精品视频免费网站 | 亚洲天堂午夜 | 97se亚洲综合在线韩国专区福利 | 久久精品23 | 精品一区二区三区在线观看视频 | 国产亚洲精品成人a | 中文字幕天堂在线 | 成人午夜免费网站 | 国产精品一区二区视频 | 在线a毛片免费视频观看 | 国产美女白浆 | 亚洲小视频在线 | 日本成人一区 | 国产91成人 | 精品黑人一区二区三区国语馆 | 制服丝袜日日夜夜 | 亚洲第五色综合网 | 久久精品中文字幕一区二区三区 | 久久国语对白 | 成人午夜免费国产 | 免费视频a|