httpclient get和post,已解决中文乱码

  public String get(String url1) { 
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String context="";
        try { 
        URL url = new URL(url1);
URI uri = new URI(url.getProtocol(), url.getHost() + ":" + url.getPort(),url.getPath(), url.getQuery(), null);
            // ����httpget.   
            HttpGet httpget = new HttpGet(uri); 
            System.out.println("executing request " + httpget.getURI()); 
            // ִ��get����.   
            CloseableHttpResponse response = httpclient.execute(httpget); 
            // ��ȡ��Ӧʵ��   
            HttpEntity entity = response.getEntity(); 
            // ��ӡ��Ӧ״̬   
            System.out.println(response.getStatusLine()); 
            if (entity != null) { 
               // ��ӡ��Ӧ����   
            context = EntityUtils.toString(entity, "UTF-8");
            System.out.println(context);               
            } 
           response.close();  
           return context;           
        } catch (Exception e) { 
            e.printStackTrace();
            return "Exception";
        }
        finally { 
   //          �ر�����,�ͷ���Դ   
            try { 
                httpclient.close(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
        }
    }



  public String post(String url,JSONObject param) {
    String context="";
        // ����Ĭ�ϵ�httpClientʵ��.   
        CloseableHttpClient httpclient = HttpClients.createDefault(); 
          try {
        URL url1 = new URL(url);  
            URI uri = new URI(url1.getProtocol(), url1.getHost(), url1.getPath(), url1.getQuery(), null);
        // ����httppost   
        HttpPost httppost = new HttpPost(uri); 
        // �

猜你喜欢

转载自zhouying1993.iteye.com/blog/2298743