接口 两种调用http接口的方法

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
   
   /*
      第一种:httpClient 方式
      所需jar包: commons-httpclient-3.0.1.jar  及其他依赖jar包
    */
   public static void sendPostRequest1(Map paramMap,String REQUEST_URL)
    {
        try {
            HttpClient httpclient = new HttpClient();
            httpclient.getParams().setHttpElementCharset("utf-8");
            httpclient.getParams().setContentCharset("utf-8");
            httpclient.getParams().setSoTimeout(10000);

            PostMethod post=new PostMethod(REQUEST_URL);
			//参数数组
            NameValuePair[] params=new NameValuePair[paramMap.size()];

            int index=0;
            for(String key : paramMap.keySet())
            {
                String value=paramMap.get(key);
                NameValuePair n=new NameValuePair(key,value);
                params[index]=n;
                index++;
            }
            /*
              另一种添加参数的方法:
              post.addParameter(key, value);
            */
            //设置请求参数
            post.setRequestBody(params);
			//开始请求
            httpclient.executeMethod(post);

			// 判断请求响应状态码,状态码为200表示服务端成功响应了客户端的请求 
            if(post.getStatusCode()==200)
	        {
				//获取返回消息字符串
			    String str=post.getResponseBodyAsString();
			    System.out.println(str);
	        }
           
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


   /*
      第二种:HttpPost 方式
      所需jar包: httpclient-4.3.5.jar、httpcore-4.3.2.jar  及其他依赖jar包
    */
    public static void sendPostRequest2(Map paramMap,String REQUEST_URL)
    {
        try {

            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(REQUEST_URL);
            //设置请求和传输超时时间
			RequestConfig config=RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
			httpPost.setConfig(config);
            List list=new ArrayList();
            for(String key : paramMap.keySet())
            {
                String value=paramMap.get(key);
                BasicNameValuePair  n=new BasicNameValuePair(key,value);
                list.add(n);
           
            }
			//设置参数
            HttpEntity httpEntity=new UrlEncodedFormEntity(list,HTTP.UTF_8);
            httpPost.setEntity(httpEntity);
			//开始请求
            HttpResponse response=httpclient.execute(httpPost);

			// 判断请求响应状态码,状态码为200表示服务端成功响应了客户端的请求 
            if(response.getStatusLine().getStatusCode()==200)
            {
				//获取返回消息字符串
                String result=EntityUtils.toString(response.getEntity());
               
                System.out.println(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

 

你可能感兴趣的:(Java)