zoukankan      html  css  js  c++  java
  • java 常见几种发送http请求案例

      1 import java.io.FileOutputStream;  
      2 import java.io.IOException;  
      3 import java.io.InputStream;  
      4 import java.io.InputStreamReader;  
      5 import java.io.OutputStreamWriter;  
      6 import java.io.UnsupportedEncodingException;  
      7 import java.net.HttpURLConnection;  
      8 import java.net.Socket;  
      9 import java.net.URL;  
     10 import java.net.URLConnection;  
     11 import java.net.URLEncoder;  
     12 import java.util.ArrayList;  
     13 import java.util.HashMap;  
     14 import java.util.Iterator;  
     15 import java.util.List;  
     16 import java.util.Map;  
     17 import java.util.Map.Entry;  
     18   
     19 import org.apache.http.HttpResponse;  
     20 import org.apache.http.NameValuePair;  
     21 import org.apache.http.client.HttpClient;  
     22 import org.apache.http.client.entity.UrlEncodedFormEntity;  
     23 import org.apache.http.client.methods.HttpGet;  
     24 import org.apache.http.client.methods.HttpPost;  
     25 import org.apache.http.impl.client.DefaultHttpClient;  
     26 import org.apache.http.message.BasicNameValuePair;  
     27   
     28 /** 
     29  * @Description:发送http请求帮助类 
     30  * @author:liuyc 
     31  * @time:2016年5月17日 下午3:25:32 
     32  */  
     33 public class HttpClientHelper {  
     34     /** 
     35      * @Description:使用HttpURLConnection发送post请求 
     36      * @author:liuyc 
     37      * @time:2016年5月17日 下午3:26:07 
     38      */  
     39     public static String sendPost(String urlParam, Map<String, Object> params, String charset) {  
     40         StringBuffer resultBuffer = null;  
     41         // 构建请求参数  
     42         StringBuffer sbParams = new StringBuffer();  
     43         if (params != null && params.size() > 0) {  
     44             for (Entry<String, Object> e : params.entrySet()) {  
     45                 sbParams.append(e.getKey());  
     46                 sbParams.append("=");  
     47                 sbParams.append(e.getValue());  
     48                 sbParams.append("&");  
     49             }  
     50         }  
     51         HttpURLConnection con = null;  
     52         OutputStreamWriter osw = null;  
     53         BufferedReader br = null;  
     54         // 发送请求  
     55         try {  
     56             URL url = new URL(urlParam);  
     57             con = (HttpURLConnection) url.openConnection();  
     58             con.setRequestMethod("POST");  
     59             con.setDoOutput(true);  
     60             con.setDoInput(true);  
     61             con.setUseCaches(false);  
     62             con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
     63             if (sbParams != null && sbParams.length() > 0) {  
     64                 osw = new OutputStreamWriter(con.getOutputStream(), charset);  
     65                 osw.write(sbParams.substring(0, sbParams.length() - 1));  
     66                 osw.flush();  
     67             }  
     68             // 读取返回内容  
     69             resultBuffer = new StringBuffer();  
     70             int contentLength = Integer.parseInt(con.getHeaderField("Content-Length"));  
     71             if (contentLength > 0) {  
     72                 br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));  
     73                 String temp;  
     74                 while ((temp = br.readLine()) != null) {  
     75                     resultBuffer.append(temp);  
     76                 }  
     77             }  
     78         } catch (Exception e) {  
     79             throw new RuntimeException(e);  
     80         } finally {  
     81             if (osw != null) {  
     82                 try {  
     83                     osw.close();  
     84                 } catch (IOException e) {  
     85                     osw = null;  
     86                     throw new RuntimeException(e);  
     87                 } finally {  
     88                     if (con != null) {  
     89                         con.disconnect();  
     90                         con = null;  
     91                     }  
     92                 }  
     93             }  
     94             if (br != null) {  
     95                 try {  
     96                     br.close();  
     97                 } catch (IOException e) {  
     98                     br = null;  
     99                     throw new RuntimeException(e);  
    100                 } finally {  
    101                     if (con != null) {  
    102                         con.disconnect();  
    103                         con = null;  
    104                     }  
    105                 }  
    106             }  
    107         }  
    108   
    109         return resultBuffer.toString();  
    110     }  
    111   
    112     /** 
    113      * @Description:使用URLConnection发送post 
    114      * @author:liuyc 
    115      * @time:2016年5月17日 下午3:26:52 
    116      */  
    117     public static String sendPost2(String urlParam, Map<String, Object> params, String charset) {  
    118         StringBuffer resultBuffer = null;  
    119         // 构建请求参数  
    120         StringBuffer sbParams = new StringBuffer();  
    121         if (params != null && params.size() > 0) {  
    122             for (Entry<String, Object> e : params.entrySet()) {  
    123                 sbParams.append(e.getKey());  
    124                 sbParams.append("=");  
    125                 sbParams.append(e.getValue());  
    126                 sbParams.append("&");  
    127             }  
    128         }  
    129         URLConnection con = null;  
    130         OutputStreamWriter osw = null;  
    131         BufferedReader br = null;  
    132         try {  
    133             URL realUrl = new URL(urlParam);  
    134             // 打开和URL之间的连接  
    135             con = realUrl.openConnection();  
    136             // 设置通用的请求属性  
    137             con.setRequestProperty("accept", "*/*");  
    138             con.setRequestProperty("connection", "Keep-Alive");  
    139             con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
    140             con.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");  
    141             // 发送POST请求必须设置如下两行  
    142             con.setDoOutput(true);  
    143             con.setDoInput(true);  
    144             // 获取URLConnection对象对应的输出流  
    145             osw = new OutputStreamWriter(con.getOutputStream(), charset);  
    146             if (sbParams != null && sbParams.length() > 0) {  
    147                 // 发送请求参数  
    148                 osw.write(sbParams.substring(0, sbParams.length() - 1));  
    149                 // flush输出流的缓冲  
    150                 osw.flush();  
    151             }  
    152             // 定义BufferedReader输入流来读取URL的响应  
    153             resultBuffer = new StringBuffer();  
    154             int contentLength = Integer.parseInt(con.getHeaderField("Content-Length"));  
    155             if (contentLength > 0) {  
    156                 br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));  
    157                 String temp;  
    158                 while ((temp = br.readLine()) != null) {  
    159                     resultBuffer.append(temp);  
    160                 }  
    161             }  
    162         } catch (Exception e) {  
    163             throw new RuntimeException(e);  
    164         } finally {  
    165             if (osw != null) {  
    166                 try {  
    167                     osw.close();  
    168                 } catch (IOException e) {  
    169                     osw = null;  
    170                     throw new RuntimeException(e);  
    171                 }  
    172             }  
    173             if (br != null) {  
    174                 try {  
    175                     br.close();  
    176                 } catch (IOException e) {  
    177                     br = null;  
    178                     throw new RuntimeException(e);  
    179                 }  
    180             }  
    181         }  
    182         return resultBuffer.toString();  
    183     }  
    184   
    185     /**  
    186      * @Description:发送get请求保存下载文件  
    187      * @author:liuyc  
    188      * @time:2016年5月17日 下午3:27:29  
    189      */  
    190     public static void sendGetAndSaveFile(String urlParam, Map<String, Object> params, String fileSavePath) {  
    191         // 构建请求参数  
    192         StringBuffer sbParams = new StringBuffer();  
    193         if (params != null && params.size() > 0) {  
    194             for (Entry<String, Object> entry : params.entrySet()) {  
    195                 sbParams.append(entry.getKey());  
    196                 sbParams.append("=");  
    197                 sbParams.append(entry.getValue());  
    198                 sbParams.append("&");  
    199             }  
    200         }  
    201         HttpURLConnection con = null;  
    202         BufferedReader br = null;  
    203         FileOutputStream os = null;  
    204         try {  
    205             URL url = null;  
    206             if (sbParams != null && sbParams.length() > 0) {  
    207                 url = new URL(urlParam + "?" + sbParams.substring(0, sbParams.length() - 1));  
    208             } else {  
    209                 url = new URL(urlParam);  
    210             }  
    211             con = (HttpURLConnection) url.openConnection();  
    212             con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
    213             con.connect();  
    214             InputStream is = con.getInputStream();  
    215             os = new FileOutputStream(fileSavePath);  
    216             byte buf[] = new byte[1024];  
    217             int count = 0;  
    218             while ((count = is.read(buf)) != -1) {  
    219                 os.write(buf, 0, count);  
    220             }  
    221             os.flush();  
    222         } catch (Exception e) {  
    223             throw new RuntimeException(e);  
    224         } finally {  
    225             if (os != null) {  
    226                 try {  
    227                     os.close();  
    228                 } catch (IOException e) {  
    229                     os = null;  
    230                     throw new RuntimeException(e);  
    231                 } finally {  
    232                     if (con != null) {  
    233                         con.disconnect();  
    234                         con = null;  
    235                     }  
    236                 }  
    237             }  
    238             if (br != null) {  
    239                 try {  
    240                     br.close();  
    241                 } catch (IOException e) {  
    242                     br = null;  
    243                     throw new RuntimeException(e);  
    244                 } finally {  
    245                     if (con != null) {  
    246                         con.disconnect();  
    247                         con = null;  
    248                     }  
    249                 }  
    250             }  
    251         }  
    252     }  
    253   
    254     /** 
    255      * @Description:使用HttpURLConnection发送get请求 
    256      * @author:liuyc 
    257      * @time:2016年5月17日 下午3:27:29 
    258      */  
    259     public static String sendGet(String urlParam, Map<String, Object> params, String charset) {  
    260         StringBuffer resultBuffer = null;  
    261         // 构建请求参数  
    262         StringBuffer sbParams = new StringBuffer();  
    263         if (params != null && params.size() > 0) {  
    264             for (Entry<String, Object> entry : params.entrySet()) {  
    265                 sbParams.append(entry.getKey());  
    266                 sbParams.append("=");  
    267                 sbParams.append(entry.getValue());  
    268                 sbParams.append("&");  
    269             }  
    270         }  
    271         HttpURLConnection con = null;  
    272         BufferedReader br = null;  
    273         try {  
    274             URL url = null;  
    275             if (sbParams != null && sbParams.length() > 0) {  
    276                 url = new URL(urlParam + "?" + sbParams.substring(0, sbParams.length() - 1));  
    277             } else {  
    278                 url = new URL(urlParam);  
    279             }  
    280             con = (HttpURLConnection) url.openConnection();  
    281             con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
    282             con.connect();  
    283             resultBuffer = new StringBuffer();  
    284             br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));  
    285             String temp;  
    286             while ((temp = br.readLine()) != null) {  
    287                 resultBuffer.append(temp);  
    288             }  
    289         } catch (Exception e) {  
    290             throw new RuntimeException(e);  
    291         } finally {  
    292             if (br != null) {  
    293                 try {  
    294                     br.close();  
    295                 } catch (IOException e) {  
    296                     br = null;  
    297                     throw new RuntimeException(e);  
    298                 } finally {  
    299                     if (con != null) {  
    300                         con.disconnect();  
    301                         con = null;  
    302                     }  
    303                 }  
    304             }  
    305         }  
    306         return resultBuffer.toString();  
    307     }  
    308   
    309     /** 
    310      * @Description:使用URLConnection发送get请求 
    311      * @author:liuyc 
    312      * @time:2016年5月17日 下午3:27:58 
    313      */  
    314     public static String sendGet2(String urlParam, Map<String, Object> params, String charset) {  
    315         StringBuffer resultBuffer = null;  
    316         // 构建请求参数  
    317         StringBuffer sbParams = new StringBuffer();  
    318         if (params != null && params.size() > 0) {  
    319             for (Entry<String, Object> entry : params.entrySet()) {  
    320                 sbParams.append(entry.getKey());  
    321                 sbParams.append("=");  
    322                 sbParams.append(entry.getValue());  
    323                 sbParams.append("&");  
    324             }  
    325         }  
    326         BufferedReader br = null;  
    327         try {  
    328             URL url = null;  
    329             if (sbParams != null && sbParams.length() > 0) {  
    330                 url = new URL(urlParam + "?" + sbParams.substring(0, sbParams.length() - 1));  
    331             } else {  
    332                 url = new URL(urlParam);  
    333             }  
    334             URLConnection con = url.openConnection();  
    335             // 设置请求属性  
    336             con.setRequestProperty("accept", "*/*");  
    337             con.setRequestProperty("connection", "Keep-Alive");  
    338             con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
    339             con.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");  
    340             // 建立连接  
    341             con.connect();  
    342             resultBuffer = new StringBuffer();  
    343             br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));  
    344             String temp;  
    345             while ((temp = br.readLine()) != null) {  
    346                 resultBuffer.append(temp);  
    347             }  
    348         } catch (Exception e) {  
    349             throw new RuntimeException(e);  
    350         } finally {  
    351             if (br != null) {  
    352                 try {  
    353                     br.close();  
    354                 } catch (IOException e) {  
    355                     br = null;  
    356                     throw new RuntimeException(e);  
    357                 }  
    358             }  
    359         }  
    360         return resultBuffer.toString();  
    361     }  
    362   
    363     /**  
    364      * @Description:使用HttpClient发送post请求  
    365      * @author:liuyc  
    366      * @time:2016年5月17日 下午3:28:23  
    367      */  
    368     public static String httpClientPost(String urlParam, Map<String, Object> params, String charset) {  
    369         StringBuffer resultBuffer = null;  
    370         HttpClient client = new DefaultHttpClient();  
    371         HttpPost httpPost = new HttpPost(urlParam);  
    372         // 构建请求参数  
    373         List<NameValuePair> list = new ArrayList<NameValuePair>();  
    374         Iterator<Entry<String, Object>> iterator = params.entrySet().iterator();  
    375         while (iterator.hasNext()) {  
    376             Entry<String, Object> elem = iterator.next();  
    377             list.add(new BasicNameValuePair(elem.getKey(), String.valueOf(elem.getValue())));  
    378         }  
    379         BufferedReader br = null;  
    380         try {  
    381             if (list.size() > 0) {  
    382                 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);  
    383                 httpPost.setEntity(entity);  
    384             }  
    385             HttpResponse response = client.execute(httpPost);  
    386             // 读取服务器响应数据  
    387             resultBuffer = new StringBuffer();  
    388             br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));  
    389             String temp;  
    390             while ((temp = br.readLine()) != null) {  
    391                 resultBuffer.append(temp);  
    392             }  
    393         } catch (Exception e) {  
    394             throw new RuntimeException(e);  
    395         } finally {  
    396             if (br != null) {  
    397                 try {  
    398                     br.close();  
    399                 } catch (IOException e) {  
    400                     br = null;  
    401                     throw new RuntimeException(e);  
    402                 }  
    403             }  
    404         }  
    405         return resultBuffer.toString();  
    406     }  
    407   
    408     /** 
    409      * @Description:使用HttpClient发送get请求 
    410      * @author:liuyc 
    411      * @time:2016年5月17日 下午3:28:56 
    412      */  
    413     public static String httpClientGet(String urlParam, Map<String, Object> params, String charset) {  
    414         StringBuffer resultBuffer = null;  
    415         HttpClient client = new DefaultHttpClient();  
    416         BufferedReader br = null;  
    417         // 构建请求参数  
    418         StringBuffer sbParams = new StringBuffer();  
    419         if (params != null && params.size() > 0) {  
    420             for (Entry<String, Object> entry : params.entrySet()) {  
    421                 sbParams.append(entry.getKey());  
    422                 sbParams.append("=");  
    423                 try {  
    424                     sbParams.append(URLEncoder.encode(String.valueOf(entry.getValue()), charset));  
    425                 } catch (UnsupportedEncodingException e) {  
    426                     throw new RuntimeException(e);  
    427                 }  
    428                 sbParams.append("&");  
    429             }  
    430         }  
    431         if (sbParams != null && sbParams.length() > 0) {  
    432             urlParam = urlParam + "?" + sbParams.substring(0, sbParams.length() - 1);  
    433         }  
    434         HttpGet httpGet = new HttpGet(urlParam);  
    435         try {  
    436             HttpResponse response = client.execute(httpGet);  
    437             // 读取服务器响应数据  
    438             br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));  
    439             String temp;  
    440             resultBuffer = new StringBuffer();  
    441             while ((temp = br.readLine()) != null) {  
    442                 resultBuffer.append(temp);  
    443             }  
    444         } catch (Exception e) {  
    445             throw new RuntimeException(e);  
    446         } finally {  
    447             if (br != null) {  
    448                 try {  
    449                     br.close();  
    450                 } catch (IOException e) {  
    451                     br = null;  
    452                     throw new RuntimeException(e);  
    453                 }  
    454             }  
    455         }  
    456         return resultBuffer.toString();  
    457     }  
    458   
    459     /** 
    460      * @Description:使用socket发送post请求 
    461      * @author:liuyc 
    462      * @time:2016年5月18日 上午9:26:22 
    463      */  
    464     public static String sendSocketPost(String urlParam, Map<String, Object> params, String charset) {  
    465         String result = "";  
    466         // 构建请求参数  
    467         StringBuffer sbParams = new StringBuffer();  
    468         if (params != null && params.size() > 0) {  
    469             for (Entry<String, Object> entry : params.entrySet()) {  
    470                 sbParams.append(entry.getKey());  
    471                 sbParams.append("=");  
    472                 sbParams.append(entry.getValue());  
    473                 sbParams.append("&");  
    474             }  
    475         }  
    476         Socket socket = null;  
    477         OutputStreamWriter osw = null;  
    478         InputStream is = null;  
    479         try {  
    480             URL url = new URL(urlParam);  
    481             String host = url.getHost();  
    482             int port = url.getPort();  
    483             if (-1 == port) {  
    484                 port = 80;  
    485             }  
    486             String path = url.getPath();  
    487             socket = new Socket(host, port);  
    488             StringBuffer sb = new StringBuffer();  
    489             sb.append("POST " + path + " HTTP/1.1
    ");  
    490             sb.append("Host: " + host + "
    ");  
    491             sb.append("Connection: Keep-Alive
    ");  
    492             sb.append("Content-Type: application/x-www-form-urlencoded; charset=utf-8 
    ");  
    493             sb.append("Content-Length: ").append(sb.toString().getBytes().length).append("
    ");  
    494             // 这里一个回车换行,表示消息头写完,不然服务器会继续等待  
    495             sb.append("
    ");  
    496             if (sbParams != null && sbParams.length() > 0) {  
    497                 sb.append(sbParams.substring(0, sbParams.length() - 1));  
    498             }  
    499             osw = new OutputStreamWriter(socket.getOutputStream());  
    500             osw.write(sb.toString());  
    501             osw.flush();  
    502             is = socket.getInputStream();  
    503             String line = null;  
    504             // 服务器响应体数据长度  
    505             int contentLength = 0;  
    506             // 读取http响应头部信息  
    507             do {  
    508                 line = readLine(is, 0, charset);  
    509                 if (line.startsWith("Content-Length")) {  
    510                     // 拿到响应体内容长度  
    511                     contentLength = Integer.parseInt(line.split(":")[1].trim());  
    512                 }  
    513                 // 如果遇到了一个单独的回车换行,则表示请求头结束  
    514             } while (!line.equals("
    "));  
    515             // 读取出响应体数据(就是你要的数据)  
    516             result = readLine(is, contentLength, charset);  
    517         } catch (Exception e) {  
    518             throw new RuntimeException(e);  
    519         } finally {  
    520             if (osw != null) {  
    521                 try {  
    522                     osw.close();  
    523                 } catch (IOException e) {  
    524                     osw = null;  
    525                     throw new RuntimeException(e);  
    526                 } finally {  
    527                     if (socket != null) {  
    528                         try {  
    529                             socket.close();  
    530                         } catch (IOException e) {  
    531                             socket = null;  
    532                             throw new RuntimeException(e);  
    533                         }  
    534                     }  
    535                 }  
    536             }  
    537             if (is != null) {  
    538                 try {  
    539                     is.close();  
    540                 } catch (IOException e) {  
    541                     is = null;  
    542                     throw new RuntimeException(e);  
    543                 } finally {  
    544                     if (socket != null) {  
    545                         try {  
    546                             socket.close();  
    547                         } catch (IOException e) {  
    548                             socket = null;  
    549                             throw new RuntimeException(e);  
    550                         }  
    551                     }  
    552                 }  
    553             }  
    554         }  
    555         return result;  
    556     }  
    557   
    558     /** 
    559      * @Description:使用socket发送get请求 
    560      * @author:liuyc 
    561      * @time:2016年5月18日 上午9:27:18 
    562      */  
    563     public static String sendSocketGet(String urlParam, Map<String, Object> params, String charset) {  
    564         String result = "";  
    565         // 构建请求参数  
    566         StringBuffer sbParams = new StringBuffer();  
    567         if (params != null && params.size() > 0) {  
    568             for (Entry<String, Object> entry : params.entrySet()) {  
    569                 sbParams.append(entry.getKey());  
    570                 sbParams.append("=");  
    571                 sbParams.append(entry.getValue());  
    572                 sbParams.append("&");  
    573             }  
    574         }  
    575         Socket socket = null;  
    576         OutputStreamWriter osw = null;  
    577         InputStream is = null;  
    578         try {  
    579             URL url = new URL(urlParam);  
    580             String host = url.getHost();  
    581             int port = url.getPort();  
    582             if (-1 == port) {  
    583                 port = 80;  
    584             }  
    585             String path = url.getPath();  
    586             socket = new Socket(host, port);  
    587             StringBuffer sb = new StringBuffer();  
    588             sb.append("GET " + path + " HTTP/1.1
    ");  
    589             sb.append("Host: " + host + "
    ");  
    590             sb.append("Connection: Keep-Alive
    ");  
    591             sb.append("Content-Type: application/x-www-form-urlencoded; charset=utf-8 
    ");  
    592             sb.append("Content-Length: ").append(sb.toString().getBytes().length).append("
    ");  
    593             // 这里一个回车换行,表示消息头写完,不然服务器会继续等待  
    594             sb.append("
    ");  
    595             if (sbParams != null && sbParams.length() > 0) {  
    596                 sb.append(sbParams.substring(0, sbParams.length() - 1));  
    597             }  
    598             osw = new OutputStreamWriter(socket.getOutputStream());  
    599             osw.write(sb.toString());  
    600             osw.flush();  
    601             is = socket.getInputStream();  
    602             String line = null;  
    603             // 服务器响应体数据长度  
    604             int contentLength = 0;  
    605             // 读取http响应头部信息  
    606             do {  
    607                 line = readLine(is, 0, charset);  
    608                 if (line.startsWith("Content-Length")) {  
    609                     // 拿到响应体内容长度  
    610                     contentLength = Integer.parseInt(line.split(":")[1].trim());  
    611                 }  
    612                 // 如果遇到了一个单独的回车换行,则表示请求头结束  
    613             } while (!line.equals("
    "));  
    614             // 读取出响应体数据(就是你要的数据)  
    615             result = readLine(is, contentLength, charset);  
    616         } catch (Exception e) {  
    617             throw new RuntimeException(e);  
    618         } finally {  
    619             if (osw != null) {  
    620                 try {  
    621                     osw.close();  
    622                 } catch (IOException e) {  
    623                     osw = null;  
    624                     throw new RuntimeException(e);  
    625                 } finally {  
    626                     if (socket != null) {  
    627                         try {  
    628                             socket.close();  
    629                         } catch (IOException e) {  
    630                             socket = null;  
    631                             throw new RuntimeException(e);  
    632                         }  
    633                     }  
    634                 }  
    635             }  
    636             if (is != null) {  
    637                 try {  
    638                     is.close();  
    639                 } catch (IOException e) {  
    640                     is = null;  
    641                     throw new RuntimeException(e);  
    642                 } finally {  
    643                     if (socket != null) {  
    644                         try {  
    645                             socket.close();  
    646                         } catch (IOException e) {  
    647                             socket = null;  
    648                             throw new RuntimeException(e);  
    649                         }  
    650                     }  
    651                 }  
    652             }  
    653         }  
    654         return result;  
    655     }  
    656   
    657     /** 
    658      * @Description:读取一行数据,contentLe内容长度为0时,读取响应头信息,不为0时读正文 
    659      * @time:2016年5月17日 下午6:11:07 
    660      */  
    661     private static String readLine(InputStream is, int contentLength, String charset) throws IOException {  
    662         List<Byte> lineByte = new ArrayList<Byte>();  
    663         byte tempByte;  
    664         int cumsum = 0;  
    665         if (contentLength != 0) {  
    666             do {  
    667                 tempByte = (byte) is.read();  
    668                 lineByte.add(Byte.valueOf(tempByte));  
    669                 cumsum++;  
    670             } while (cumsum < contentLength);// cumsum等于contentLength表示已读完  
    671         } else {  
    672             do {  
    673                 tempByte = (byte) is.read();  
    674                 lineByte.add(Byte.valueOf(tempByte));  
    675             } while (tempByte != 10);// 换行符的ascii码值为10  
    676         }  
    677   
    678         byte[] resutlBytes = new byte[lineByte.size()];  
    679         for (int i = 0; i < lineByte.size(); i++) {  
    680             resutlBytes[i] = (lineByte.get(i)).byteValue();  
    681         }  
    682         return new String(resutlBytes, charset);  
    683     }  
    684       
    685 }  
    1. import java.io.FileOutputStream;  
    2. import java.io.IOException;  
    3. import java.io.InputStream;  
    4. import java.io.InputStreamReader;  
    5. import java.io.OutputStreamWriter;  
    6. import java.io.UnsupportedEncodingException;  
    7. import java.net.HttpURLConnection;  
    8. import java.net.Socket;  
    9. import java.net.URL;  
    10. import java.net.URLConnection;  
    11. import java.net.URLEncoder;  
    12. import java.util.ArrayList;  
    13. import java.util.HashMap;  
    14. import java.util.Iterator;  
    15. import java.util.List;  
    16. import java.util.Map;  
    17. import java.util.Map.Entry;  
    18.   
    19. import org.apache.http.HttpResponse;  
    20. import org.apache.http.NameValuePair;  
    21. import org.apache.http.client.HttpClient;  
    22. import org.apache.http.client.entity.UrlEncodedFormEntity;  
    23. import org.apache.http.client.methods.HttpGet;  
    24. import org.apache.http.client.methods.HttpPost;  
    25. import org.apache.http.impl.client.DefaultHttpClient;  
    26. import org.apache.http.message.BasicNameValuePair;  
    27.   
    28. /** 
    29.  * @Description:发送http请求帮助类 
    30.  * @author:liuyc 
    31.  * @time:2016年5月17日 下午3:25:32 
    32.  */  
    33. public class HttpClientHelper {  
    34.     /** 
    35.      * @Description:使用HttpURLConnection发送post请求 
    36.      * @author:liuyc 
    37.      * @time:2016年5月17日 下午3:26:07 
    38.      */  
    39.     public static String sendPost(String urlParam, Map<String, Object> params, String charset) {  
    40.         StringBuffer resultBuffer = null;  
    41.         // 构建请求参数  
    42.         StringBuffer sbParams = new StringBuffer();  
    43.         if (params != null && params.size() > 0) {  
    44.             for (Entry<String, Object> e : params.entrySet()) {  
    45.                 sbParams.append(e.getKey());  
    46.                 sbParams.append("=");  
    47.                 sbParams.append(e.getValue());  
    48.                 sbParams.append("&");  
    49.             }  
    50.         }  
    51.         HttpURLConnection con = null;  
    52.         OutputStreamWriter osw = null;  
    53.         BufferedReader br = null;  
    54.         // 发送请求  
    55.         try {  
    56.             URL url = new URL(urlParam);  
    57.             con = (HttpURLConnection) url.openConnection();  
    58.             con.setRequestMethod("POST");  
    59.             con.setDoOutput(true);  
    60.             con.setDoInput(true);  
    61.             con.setUseCaches(false);  
    62.             con.setRequestProperty("Content-Type""application/x-www-form-urlencoded");  
    63.             if (sbParams != null && sbParams.length() > 0) {  
    64.                 osw = new OutputStreamWriter(con.getOutputStream(), charset);  
    65.                 osw.write(sbParams.substring(0, sbParams.length() - 1));  
    66.                 osw.flush();  
    67.             }  
    68.             // 读取返回内容  
    69.             resultBuffer = new StringBuffer();  
    70.             int contentLength = Integer.parseInt(con.getHeaderField("Content-Length"));  
    71.             if (contentLength > 0) {  
    72.                 br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));  
    73.                 String temp;  
    74.                 while ((temp = br.readLine()) != null) {  
    75.                     resultBuffer.append(temp);  
    76.                 }  
    77.             }  
    78.         } catch (Exception e) {  
    79.             throw new RuntimeException(e);  
    80.         } finally {  
    81.             if (osw != null) {  
    82.                 try {  
    83.                     osw.close();  
    84.                 } catch (IOException e) {  
    85.                     osw = null;  
    86.                     throw new RuntimeException(e);  
    87.                 } finally {  
    88.                     if (con != null) {  
    89.                         con.disconnect();  
    90.                         con = null;  
    91.                     }  
    92.                 }  
    93.             }  
    94.             if (br != null) {  
    95.                 try {  
    96.                     br.close();  
    97.                 } catch (IOException e) {  
    98.                     br = null;  
    99.                     throw new RuntimeException(e);  
    100.                 } finally {  
    101.                     if (con != null) {  
    102.                         con.disconnect();  
    103.                         con = null;  
    104.                     }  
    105.                 }  
    106.             }  
    107.         }  
    108.   
    109.         return resultBuffer.toString();  
    110.     }  
    111.   
    112.     /** 
    113.      * @Description:使用URLConnection发送post 
    114.      * @author:liuyc 
    115.      * @time:2016年5月17日 下午3:26:52 
    116.      */  
    117.     public static String sendPost2(String urlParam, Map<String, Object> params, String charset) {  
    118.         StringBuffer resultBuffer = null;  
    119.         // 构建请求参数  
    120.         StringBuffer sbParams = new StringBuffer();  
    121.         if (params != null && params.size() > 0) {  
    122.             for (Entry<String, Object> e : params.entrySet()) {  
    123.                 sbParams.append(e.getKey());  
    124.                 sbParams.append("=");  
    125.                 sbParams.append(e.getValue());  
    126.                 sbParams.append("&");  
    127.             }  
    128.         }  
    129.         URLConnection con = null;  
    130.         OutputStreamWriter osw = null;  
    131.         BufferedReader br = null;  
    132.         try {  
    133.             URL realUrl = new URL(urlParam);  
    134.             // 打开和URL之间的连接  
    135.             con = realUrl.openConnection();  
    136.             // 设置通用的请求属性  
    137.             con.setRequestProperty("accept""*/*");  
    138.             con.setRequestProperty("connection""Keep-Alive");  
    139.             con.setRequestProperty("Content-Type""application/x-www-form-urlencoded");  
    140.             con.setRequestProperty("user-agent""Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");  
    141.             // 发送POST请求必须设置如下两行  
    142.             con.setDoOutput(true);  
    143.             con.setDoInput(true);  
    144.             // 获取URLConnection对象对应的输出流  
    145.             osw = new OutputStreamWriter(con.getOutputStream(), charset);  
    146.             if (sbParams != null && sbParams.length() > 0) {  
    147.                 // 发送请求参数  
    148.                 osw.write(sbParams.substring(0, sbParams.length() - 1));  
    149.                 // flush输出流的缓冲  
    150.                 osw.flush();  
    151.             }  
    152.             // 定义BufferedReader输入流来读取URL的响应  
    153.             resultBuffer = new StringBuffer();  
    154.             int contentLength = Integer.parseInt(con.getHeaderField("Content-Length"));  
    155.             if (contentLength > 0) {  
    156.                 br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));  
    157.                 String temp;  
    158.                 while ((temp = br.readLine()) != null) {  
    159.                     resultBuffer.append(temp);  
    160.                 }  
    161.             }  
    162.         } catch (Exception e) {  
    163.             throw new RuntimeException(e);  
    164.         } finally {  
    165.             if (osw != null) {  
    166.                 try {  
    167.                     osw.close();  
    168.                 } catch (IOException e) {  
    169.                     osw = null;  
    170.                     throw new RuntimeException(e);  
    171.                 }  
    172.             }  
    173.             if (br != null) {  
    174.                 try {  
    175.                     br.close();  
    176.                 } catch (IOException e) {  
    177.                     br = null;  
    178.                     throw new RuntimeException(e);  
    179.                 }  
    180.             }  
    181.         }  
    182.         return resultBuffer.toString();  
    183.     }  
    184.   
    185.     /**  
    186.      * @Description:发送get请求保存下载文件  
    187.      * @author:liuyc  
    188.      * @time:2016517日 下午3:27:29  
    189.      */  
    190.     public static void sendGetAndSaveFile(String urlParam, Map<String, Object> params, String fileSavePath) {  
    191.         // 构建请求参数  
    192.         StringBuffer sbParams = new StringBuffer();  
    193.         if (params != null && params.size() > 0) {  
    194.             for (Entry<String, Object> entry : params.entrySet()) {  
    195.                 sbParams.append(entry.getKey());  
    196.                 sbParams.append("=");  
    197.                 sbParams.append(entry.getValue());  
    198.                 sbParams.append("&");  
    199.             }  
    200.         }  
    201.         HttpURLConnection con = null;  
    202.         BufferedReader br = null;  
    203.         FileOutputStream os = null;  
    204.         try {  
    205.             URL url = null;  
    206.             if (sbParams != null && sbParams.length() > 0) {  
    207.                 url = new URL(urlParam + "?" + sbParams.substring(0, sbParams.length() - 1));  
    208.             } else {  
    209.                 url = new URL(urlParam);  
    210.             }  
    211.             con = (HttpURLConnection) url.openConnection();  
    212.             con.setRequestProperty("Content-Type""application/x-www-form-urlencoded");  
    213.             con.connect();  
    214.             InputStream is = con.getInputStream();  
    215.             os = new FileOutputStream(fileSavePath);  
    216.             byte buf[] = new byte[1024];  
    217.             int count = 0;  
    218.             while ((count = is.read(buf)) != -1) {  
    219.                 os.write(buf, 0, count);  
    220.             }  
    221.             os.flush();  
    222.         } catch (Exception e) {  
    223.             throw new RuntimeException(e);  
    224.         } finally {  
    225.             if (os != null) {  
    226.                 try {  
    227.                     os.close();  
    228.                 } catch (IOException e) {  
    229.                     os = null;  
    230.                     throw new RuntimeException(e);  
    231.                 } finally {  
    232.                     if (con != null) {  
    233.                         con.disconnect();  
    234.                         con = null;  
    235.                     }  
    236.                 }  
    237.             }  
    238.             if (br != null) {  
    239.                 try {  
    240.                     br.close();  
    241.                 } catch (IOException e) {  
    242.                     br = null;  
    243.                     throw new RuntimeException(e);  
    244.                 } finally {  
    245.                     if (con != null) {  
    246.                         con.disconnect();  
    247.                         con = null;  
    248.                     }  
    249.                 }  
    250.             }  
    251.         }  
    252.     }  
    253.   
    254.     /** 
    255.      * @Description:使用HttpURLConnection发送get请求 
    256.      * @author:liuyc 
    257.      * @time:2016年5月17日 下午3:27:29 
    258.      */  
    259.     public static String sendGet(String urlParam, Map<String, Object> params, String charset) {  
    260.         StringBuffer resultBuffer = null;  
    261.         // 构建请求参数  
    262.         StringBuffer sbParams = new StringBuffer();  
    263.         if (params != null && params.size() > 0) {  
    264.             for (Entry<String, Object> entry : params.entrySet()) {  
    265.                 sbParams.append(entry.getKey());  
    266.                 sbParams.append("=");  
    267.                 sbParams.append(entry.getValue());  
    268.                 sbParams.append("&");  
    269.             }  
    270.         }  
    271.         HttpURLConnection con = null;  
    272.         BufferedReader br = null;  
    273.         try {  
    274.             URL url = null;  
    275.             if (sbParams != null && sbParams.length() > 0) {  
    276.                 url = new URL(urlParam + "?" + sbParams.substring(0, sbParams.length() - 1));  
    277.             } else {  
    278.                 url = new URL(urlParam);  
    279.             }  
    280.             con = (HttpURLConnection) url.openConnection();  
    281.             con.setRequestProperty("Content-Type""application/x-www-form-urlencoded");  
    282.             con.connect();  
    283.             resultBuffer = new StringBuffer();  
    284.             br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));  
    285.             String temp;  
    286.             while ((temp = br.readLine()) != null) {  
    287.                 resultBuffer.append(temp);  
    288.             }  
    289.         } catch (Exception e) {  
    290.             throw new RuntimeException(e);  
    291.         } finally {  
    292.             if (br != null) {  
    293.                 try {  
    294.                     br.close();  
    295.                 } catch (IOException e) {  
    296.                     br = null;  
    297.                     throw new RuntimeException(e);  
    298.                 } finally {  
    299.                     if (con != null) {  
    300.                         con.disconnect();  
    301.                         con = null;  
    302.                     }  
    303.                 }  
    304.             }  
    305.         }  
    306.         return resultBuffer.toString();  
    307.     }  
    308.   
    309.     /** 
    310.      * @Description:使用URLConnection发送get请求 
    311.      * @author:liuyc 
    312.      * @time:2016年5月17日 下午3:27:58 
    313.      */  
    314.     public static String sendGet2(String urlParam, Map<String, Object> params, String charset) {  
    315.         StringBuffer resultBuffer = null;  
    316.         // 构建请求参数  
    317.         StringBuffer sbParams = new StringBuffer();  
    318.         if (params != null && params.size() > 0) {  
    319.             for (Entry<String, Object> entry : params.entrySet()) {  
    320.                 sbParams.append(entry.getKey());  
    321.                 sbParams.append("=");  
    322.                 sbParams.append(entry.getValue());  
    323.                 sbParams.append("&");  
    324.             }  
    325.         }  
    326.         BufferedReader br = null;  
    327.         try {  
    328.             URL url = null;  
    329.             if (sbParams != null && sbParams.length() > 0) {  
    330.                 url = new URL(urlParam + "?" + sbParams.substring(0, sbParams.length() - 1));  
    331.             } else {  
    332.                 url = new URL(urlParam);  
    333.             }  
    334.             URLConnection con = url.openConnection();  
    335.             // 设置请求属性  
    336.             con.setRequestProperty("accept""*/*");  
    337.             con.setRequestProperty("connection""Keep-Alive");  
    338.             con.setRequestProperty("Content-Type""application/x-www-form-urlencoded");  
    339.             con.setRequestProperty("user-agent""Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");  
    340.             // 建立连接  
    341.             con.connect();  
    342.             resultBuffer = new StringBuffer();  
    343.             br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));  
    344.             String temp;  
    345.             while ((temp = br.readLine()) != null) {  
    346.                 resultBuffer.append(temp);  
    347.             }  
    348.         } catch (Exception e) {  
    349.             throw new RuntimeException(e);  
    350.         } finally {  
    351.             if (br != null) {  
    352.                 try {  
    353.                     br.close();  
    354.                 } catch (IOException e) {  
    355.                     br = null;  
    356.                     throw new RuntimeException(e);  
    357.                 }  
    358.             }  
    359.         }  
    360.         return resultBuffer.toString();  
    361.     }  
    362.   
    363.     /**  
    364.      * @Description:使用HttpClient发送post请求  
    365.      * @author:liuyc  
    366.      * @time:2016517日 下午3:28:23  
    367.      */  
    368.     public static String httpClientPost(String urlParam, Map<String, Object> params, String charset) {  
    369.         StringBuffer resultBuffer = null;  
    370.         HttpClient client = new DefaultHttpClient();  
    371.         HttpPost httpPost = new HttpPost(urlParam);  
    372.         // 构建请求参数  
    373.         List<NameValuePair> list = new ArrayList<NameValuePair>();  
    374.         Iterator<Entry<String, Object>> iterator = params.entrySet().iterator();  
    375.         while (iterator.hasNext()) {  
    376.             Entry<String, Object> elem = iterator.next();  
    377.             list.add(new BasicNameValuePair(elem.getKey(), String.valueOf(elem.getValue())));  
    378.         }  
    379.         BufferedReader br = null;  
    380.         try {  
    381.             if (list.size() > 0) {  
    382.                 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);  
    383.                 httpPost.setEntity(entity);  
    384.             }  
    385.             HttpResponse response = client.execute(httpPost);  
    386.             // 读取服务器响应数据  
    387.             resultBuffer = new StringBuffer();  
    388.             br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));  
    389.             String temp;  
    390.             while ((temp = br.readLine()) != null) {  
    391.                 resultBuffer.append(temp);  
    392.             }  
    393.         } catch (Exception e) {  
    394.             throw new RuntimeException(e);  
    395.         } finally {  
    396.             if (br != null) {  
    397.                 try {  
    398.                     br.close();  
    399.                 } catch (IOException e) {  
    400.                     br = null;  
    401.                     throw new RuntimeException(e);  
    402.                 }  
    403.             }  
    404.         }  
    405.         return resultBuffer.toString();  
    406.     }  
    407.   
    408.     /** 
    409.      * @Description:使用HttpClient发送get请求 
    410.      * @author:liuyc 
    411.      * @time:2016年5月17日 下午3:28:56 
    412.      */  
    413.     public static String httpClientGet(String urlParam, Map<String, Object> params, String charset) {  
    414.         StringBuffer resultBuffer = null;  
    415.         HttpClient client = new DefaultHttpClient();  
    416.         BufferedReader br = null;  
    417.         // 构建请求参数  
    418.         StringBuffer sbParams = new StringBuffer();  
    419.         if (params != null && params.size() > 0) {  
    420.             for (Entry<String, Object> entry : params.entrySet()) {  
    421.                 sbParams.append(entry.getKey());  
    422.                 sbParams.append("=");  
    423.                 try {  
    424.                     sbParams.append(URLEncoder.encode(String.valueOf(entry.getValue()), charset));  
    425.                 } catch (UnsupportedEncodingException e) {  
    426.                     throw new RuntimeException(e);  
    427.                 }  
    428.                 sbParams.append("&");  
    429.             }  
    430.         }  
    431.         if (sbParams != null && sbParams.length() > 0) {  
    432.             urlParam = urlParam + "?" + sbParams.substring(0, sbParams.length() - 1);  
    433.         }  
    434.         HttpGet httpGet = new HttpGet(urlParam);  
    435.         try {  
    436.             HttpResponse response = client.execute(httpGet);  
    437.             // 读取服务器响应数据  
    438.             br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));  
    439.             String temp;  
    440.             resultBuffer = new StringBuffer();  
    441.             while ((temp = br.readLine()) != null) {  
    442.                 resultBuffer.append(temp);  
    443.             }  
    444.         } catch (Exception e) {  
    445.             throw new RuntimeException(e);  
    446.         } finally {  
    447.             if (br != null) {  
    448.                 try {  
    449.                     br.close();  
    450.                 } catch (IOException e) {  
    451.                     br = null;  
    452.                     throw new RuntimeException(e);  
    453.                 }  
    454.             }  
    455.         }  
    456.         return resultBuffer.toString();  
    457.     }  
    458.   
    459.     /** 
    460.      * @Description:使用socket发送post请求 
    461.      * @author:liuyc 
    462.      * @time:2016年5月18日 上午9:26:22 
    463.      */  
    464.     public static String sendSocketPost(String urlParam, Map<String, Object> params, String charset) {  
    465.         String result = "";  
    466.         // 构建请求参数  
    467.         StringBuffer sbParams = new StringBuffer();  
    468.         if (params != null && params.size() > 0) {  
    469.             for (Entry<String, Object> entry : params.entrySet()) {  
    470.                 sbParams.append(entry.getKey());  
    471.                 sbParams.append("=");  
    472.                 sbParams.append(entry.getValue());  
    473.                 sbParams.append("&");  
    474.             }  
    475.         }  
    476.         Socket socket = null;  
    477.         OutputStreamWriter osw = null;  
    478.         InputStream is = null;  
    479.         try {  
    480.             URL url = new URL(urlParam);  
    481.             String host = url.getHost();  
    482.             int port = url.getPort();  
    483.             if (-1 == port) {  
    484.                 port = 80;  
    485.             }  
    486.             String path = url.getPath();  
    487.             socket = new Socket(host, port);  
    488.             StringBuffer sb = new StringBuffer();  
    489.             sb.append("POST " + path + " HTTP/1.1 ");  
    490.             sb.append("Host: " + host + " ");  
    491.             sb.append("Connection: Keep-Alive ");  
    492.             sb.append("Content-Type: application/x-www-form-urlencoded; charset=utf-8  ");  
    493.             sb.append("Content-Length: ").append(sb.toString().getBytes().length).append(" ");  
    494.             // 这里一个回车换行,表示消息头写完,不然服务器会继续等待  
    495.             sb.append(" ");  
    496.             if (sbParams != null && sbParams.length() > 0) {  
    497.                 sb.append(sbParams.substring(0, sbParams.length() - 1));  
    498.             }  
    499.             osw = new OutputStreamWriter(socket.getOutputStream());  
    500.             osw.write(sb.toString());  
    501.             osw.flush();  
    502.             is = socket.getInputStream();  
    503.             String line = null;  
    504.             // 服务器响应体数据长度  
    505.             int contentLength = 0;  
    506.             // 读取http响应头部信息  
    507.             do {  
    508.                 line = readLine(is, 0, charset);  
    509.                 if (line.startsWith("Content-Length")) {  
    510.                     // 拿到响应体内容长度  
    511.                     contentLength = Integer.parseInt(line.split(":")[1].trim());  
    512.                 }  
    513.                 // 如果遇到了一个单独的回车换行,则表示请求头结束  
    514.             } while (!line.equals(" "));  
    515.             // 读取出响应体数据(就是你要的数据)  
    516.             result = readLine(is, contentLength, charset);  
    517.         } catch (Exception e) {  
    518.             throw new RuntimeException(e);  
    519.         } finally {  
    520.             if (osw != null) {  
    521.                 try {  
    522.                     osw.close();  
    523.                 } catch (IOException e) {  
    524.                     osw = null;  
    525.                     throw new RuntimeException(e);  
    526.                 } finally {  
    527.                     if (socket != null) {  
    528.                         try {  
    529.                             socket.close();  
    530.                         } catch (IOException e) {  
    531.                             socket = null;  
    532.                             throw new RuntimeException(e);  
    533.                         }  
    534.                     }  
    535.                 }  
    536.             }  
    537.             if (is != null) {  
    538.                 try {  
    539.                     is.close();  
    540.                 } catch (IOException e) {  
    541.                     is = null;  
    542.                     throw new RuntimeException(e);  
    543.                 } finally {  
    544.                     if (socket != null) {  
    545.                         try {  
    546.                             socket.close();  
    547.                         } catch (IOException e) {  
    548.                             socket = null;  
    549.                             throw new RuntimeException(e);  
    550.                         }  
    551.                     }  
    552.                 }  
    553.             }  
    554.         }  
    555.         return result;  
    556.     }  
    557.   
    558.     /** 
    559.      * @Description:使用socket发送get请求 
    560.      * @author:liuyc 
    561.      * @time:2016年5月18日 上午9:27:18 
    562.      */  
    563.     public static String sendSocketGet(String urlParam, Map<String, Object> params, String charset) {  
    564.         String result = "";  
    565.         // 构建请求参数  
    566.         StringBuffer sbParams = new StringBuffer();  
    567.         if (params != null && params.size() > 0) {  
    568.             for (Entry<String, Object> entry : params.entrySet()) {  
    569.                 sbParams.append(entry.getKey());  
    570.                 sbParams.append("=");  
    571.                 sbParams.append(entry.getValue());  
    572.                 sbParams.append("&");  
    573.             }  
    574.         }  
    575.         Socket socket = null;  
    576.         OutputStreamWriter osw = null;  
    577.         InputStream is = null;  
    578.         try {  
    579.             URL url = new URL(urlParam);  
    580.             String host = url.getHost();  
    581.             int port = url.getPort();  
    582.             if (-1 == port) {  
    583.                 port = 80;  
    584.             }  
    585.             String path = url.getPath();  
    586.             socket = new Socket(host, port);  
    587.             StringBuffer sb = new StringBuffer();  
    588.             sb.append("GET " + path + " HTTP/1.1 ");  
    589.             sb.append("Host: " + host + " ");  
    590.             sb.append("Connection: Keep-Alive ");  
    591.             sb.append("Content-Type: application/x-www-form-urlencoded; charset=utf-8  ");  
    592.             sb.append("Content-Length: ").append(sb.toString().getBytes().length).append(" ");  
    593.             // 这里一个回车换行,表示消息头写完,不然服务器会继续等待  
    594.             sb.append(" ");  
    595.             if (sbParams != null && sbParams.length() > 0) {  
    596.                 sb.append(sbParams.substring(0, sbParams.length() - 1));  
    597.             }  
    598.             osw = new OutputStreamWriter(socket.getOutputStream());  
    599.             osw.write(sb.toString());  
    600.             osw.flush();  
    601.             is = socket.getInputStream();  
    602.             String line = null;  
    603.             // 服务器响应体数据长度  
    604.             int contentLength = 0;  
    605.             // 读取http响应头部信息  
    606.             do {  
    607.                 line = readLine(is, 0, charset);  
    608.                 if (line.startsWith("Content-Length")) {  
    609.                     // 拿到响应体内容长度  
    610.                     contentLength = Integer.parseInt(line.split(":")[1].trim());  
    611.                 }  
    612.                 // 如果遇到了一个单独的回车换行,则表示请求头结束  
    613.             } while (!line.equals(" "));  
    614.             // 读取出响应体数据(就是你要的数据)  
    615.             result = readLine(is, contentLength, charset);  
    616.         } catch (Exception e) {  
    617.             throw new RuntimeException(e);  
    618.         } finally {  
    619.             if (osw != null) {  
    620.                 try {  
    621.                     osw.close();  
    622.                 } catch (IOException e) {  
    623.                     osw = null;  
    624.                     throw new RuntimeException(e);  
    625.                 } finally {  
    626.                     if (socket != null) {  
    627.                         try {  
    628.                             socket.close();  
    629.                         } catch (IOException e) {  
    630.                             socket = null;  
    631.                             throw new RuntimeException(e);  
    632.                         }  
    633.                     }  
    634.                 }  
    635.             }  
    636.             if (is != null) {  
    637.                 try {  
    638.                     is.close();  
    639.                 } catch (IOException e) {  
    640.                     is = null;  
    641.                     throw new RuntimeException(e);  
    642.                 } finally {  
    643.                     if (socket != null) {  
    644.                         try {  
    645.                             socket.close();  
    646.                         } catch (IOException e) {  
    647.                             socket = null;  
    648.                             throw new RuntimeException(e);  
    649.                         }  
    650.                     }  
    651.                 }  
    652.             }  
    653.         }  
    654.         return result;  
    655.     }  
    656.   
    657.     /** 
    658.      * @Description:读取一行数据,contentLe内容长度为0时,读取响应头信息,不为0时读正文 
    659.      * @time:2016年5月17日 下午6:11:07 
    660.      */  
    661.     private static String readLine(InputStream is, int contentLength, String charset) throws IOException {  
    662.         List<Byte> lineByte = new ArrayList<Byte>();  
    663.         byte tempByte;  
    664.         int cumsum = 0;  
    665.         if (contentLength != 0) {  
    666.             do {  
    667.                 tempByte = (byte) is.read();  
    668.                 lineByte.add(Byte.valueOf(tempByte));  
    669.                 cumsum++;  
    670.             } while (cumsum < contentLength);// cumsum等于contentLength表示已读完  
    671.         } else {  
    672.             do {  
    673.                 tempByte = (byte) is.read();  
    674.                 lineByte.add(Byte.valueOf(tempByte));  
    675.             } while (tempByte != 10);// 换行符的ascii码值为10  
    676.         }  
    677.   
    678.         byte[] resutlBytes = new byte[lineByte.size()];  
    679.         for (int i = 0; i < lineByte.size(); i++) {  
    680.             resutlBytes[i] = (lineByte.get(i)).byteValue();  
    681.         }  
    682.         return new String(resutlBytes, charset);  
    683.     }  
    684.       
    685. }  
  • 相关阅读:
    jsp生成xml文件示例
    jsp分页显示
    Spring AOP学习笔记
    让leeon不再眷念马桶——书评《精通正则表达式》
    用JSP实现上传文件的两种方法
    oracle sql性能优化
    Iron Speed Designer 4.2.2学习
    再议《JavaScript代码优化一例》
    有关《大道至简》的几点讨论~
    有源则至清——我读《移山之道》
  • 原文地址:https://www.cnblogs.com/cxxjohnson/p/7816698.html
Copyright © 2011-2022 走看看