httpClient get请求

官网下载:httpcomponents-client-4.5.2-bin.zipweb

须要的 jar:httpclient-4.5.2.jar、httpclient-cache-4.5.2.jar、httpcore-4.4.4.jarapache

 使用 HttpClient 须要如下 6 个步骤: 
  1. 建立 HttpClient 的实例 
  2. 建立某种链接方法的实例,在这里是 GetMethod。在 GetMethod 的构造函数中传入待链接的地址 
  3. 调用第一步中建立好的实例的 execute 方法来执行第二步中建立好的 method 实例 
  4. 读 response 
  5. 释放链接。不管执行方法是否成功,都必须释放链接 
  6. 对获得后的内容进行处理 json

客户端代码示例:数组

public static void main(String[] args) {
        // 第一个 sendHttpPost
        HttpDemo1 httpDemo1 = new HttpDemo1();
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "httpClient Get 测试");
//        JSONObject jsonStr = new JSONObject();
//        jsonStr.put("name", "httpClient Get 测试");
        String httpUrl = "http://localhost:8080/web1/servletTestCallBack.hts";
        String result = httpDemo1.sendHttpGet(httpUrl, map);
        System.out.println("result:" + result);
    }服务器

public String sendHttpGet(String httpUrl,Map<String, String> map){
        String result = ""; 
        
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        for (Map.Entry<String, String> e : entrySet) {
            String name = e.getKey();
            String value = e.getValue();
            NameValuePair pair = new BasicNameValuePair(name, value);
            params.add(pair);
        }        
        //要传递的参数.
        String url = httpUrl+"?" + URLEncodedUtils.format(params, HTTP.UTF_8);
        
        HttpClient httpclient = new DefaultHttpClient(); 
        HttpClient httpClient = null;
        
        try{
            httpClient = new DefaultHttpClient();    
            //设置等待数据超时时间 120秒
            httpClient.getParams().setParameter("http.socket.timeout",2*1000*60);
            //设置请求超时 30秒
            httpClient.getParams().setParameter("http.connection.timeout",1*1000*30);    
            if (url == null || "".equals(url)) {
                return result;
            }
            //建立HttpGet对象
            HttpGet httpreq = new HttpGet(url);
            
            HttpResponse response = null;
            //使用DefaultHttpClient类的execute方法发送HTTP GET请求,并返回HttpResponse对象。
            response = httpclient.execute(httpreq);
            
            if (response == null) {
                return result;
            }
            //请求成功响应,读取返回数据
            if (response.getStatusLine().getStatusCode() == org.apache.http.HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity(),"utf-8");
            }else{
                System.out.println("HTTP GET请求无响应");
            } 
            
            return result;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }finally{
            try{
                httpClient.getConnectionManager().shutdown();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }socket

服务器响应代码:ide

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {函数

        request.setCharacterEncoding("UTF-8");
        //获得客户端发送过来内容的类型
        System.out.println("客户端发送过来内容的类型:" + request.getContentType());
        String name = request.getParameter("name");
        
        //获取request对象以ISO8859-1字符编码接收到的原始数据的字节数组,而后经过字节数组以指定的编码构建字符串,解决乱码问题。
        name = new String(name.getBytes("ISO8859-1"),"UTF-8");
        System.out.println("客户端传递的参数name:" + name);
        //响应客户端
        response.setCharacterEncoding("UTF-8");
        response.getWriter().print("接受到信息,ok!");
    }测试

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }this

相关文章
相关标签/搜索