zoukankan      html  css  js  c++  java
  • http实现发送post请求,获取结果集

     1 package com.ming;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.InputStreamReader;
     5 import java.io.OutputStreamWriter;
     6 import java.io.PrintWriter;
     7 import java.net.HttpURLConnection;
     8 import java.net.URL;
     9 import java.util.Map;
    10 
    11 
    12 public class HttpBaseUtil {
    13 
    14     
    15     /**
    16      * 向指定的url发送post方法,并获取放回值
    17      * @param url 发送的url
    18      * @param param 发送的参数 参数形式a=2&b=2
    19      * @param head 请求头设定
    20      */
    21     public static String sendPostUrl(String url,String param,Map<String, String> head) throws Exception{
    22         StringBuffer result=new StringBuffer();
    23         PrintWriter out =null;
    24         BufferedReader in=null;
    25         try {
    26             URL realUrl = new URL(url);
    27             // 打开和URL之间的连接
    28             HttpURLConnection  conn =(HttpURLConnection)realUrl.openConnection();
    29             // 设置通用的请求属性
    30             conn.setRequestProperty("accept", "*/*");
    31             conn.setRequestProperty("connection", "Keep-Alive");
    32             conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
    33             if(head!=null && !head.isEmpty()){
    34                 for (String key:head.keySet()){
    35                     conn.setRequestProperty(key, head.get(key));
    36                 }
    37             }
    38             conn.setDoOutput(true);
    39             conn.setDoInput(true);
    40             // 获取URLConnection对象对应的输出流
    41             out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"UTF-8"));
    42             // 发送请求参数
    43             out.print(param);
    44             // flush输出流的缓冲
    45             out.flush();
    46             // 定义BufferedReader输入流来读取URL的响应
    47             in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
    48             String line;
    49             while ((line = in.readLine()) != null) {
    50                 result.append(line);
    51             }
    52         } catch (Exception e) {
    53             throw new Exception("发送post出现异常."+e.getMessage());
    54         }
    55         return result.toString();
    56     }
    57     
    58 }
     1 import org.apache.http.HttpResponse;
     2 import org.apache.http.client.ClientProtocolException;
     3 import org.apache.http.client.HttpClient;
     4 import org.apache.http.client.ResponseHandler;
     5 import org.apache.http.client.methods.HttpUriRequest;
     6 
     7 import java.io.IOException;
     8 
     9 public class LocalHttpClient {
    10 
    11     private static LocalHttpClient localHttpClient;
    12     protected HttpClient httpClient;
    13 
    14     private static int maxTotal = 100;
    15     private static int maxPerRoute = 10;
    16     public static void init(int maxTotal,int maxPerRoute){
    17         LocalHttpClient.maxTotal = maxTotal;
    18         LocalHttpClient.maxPerRoute = maxPerRoute;
    19     }
    20 
    21     public static LocalHttpClient getInstance(){
    22         if(localHttpClient != null){
    23             return localHttpClient;
    24         }else{
    25             localHttpClient = new LocalHttpClient();
    26             if(maxTotal > 0){
    27                 localHttpClient.httpClient = HttpClientFactory.createHttpClient(maxTotal,maxPerRoute);
    28             }else{
    29                 localHttpClient.httpClient = HttpClientFactory.createHttpClient();
    30             }
    31             return localHttpClient;
    32         }
    33     }
    34 
    35     public HttpResponse execute(HttpUriRequest request){
    36         try {
    37             return httpClient.execute(request);
    38         } catch (ClientProtocolException e) {
    39             e.printStackTrace();
    40         } catch (IOException e) {
    41             e.printStackTrace();
    42         }
    43         return null;
    44     }
    45 
    46     public <T> T execute(HttpUriRequest request,ResponseHandler<T> responseHandler){
    47         try {
    48             return httpClient.execute(request, responseHandler);
    49         } catch (ClientProtocolException e) {
    50             e.printStackTrace();
    51         } catch (IOException e) {
    52             e.printStackTrace();
    53         }
    54         return null;
    55     }
    56 }
  • 相关阅读:
    C#读取Excel文档
    用DataSet方式更新数据库表
    using(){},Close(),Dispose()的区别
    ADO.NET连接数据库的两种方式
    DateUtils
    GoF的23个经典设计模式
    react中需要用到【深度复制】的问题
    css区分ie8/ie9/ie10/ie11 chrome firefox的代码
    【个人学习笔记】走近H5
    【React入门实例(运行于浏览器duan)】
  • 原文地址:https://www.cnblogs.com/huzi007/p/5048463.html
Copyright © 2011-2022 走看看