HttpUtils post

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaoleizhanghahaha/article/details/86557870

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class HttpUtils {
    private static Logger logger = LogManager.getLogger(HttpUtils.class);
    public static String submitPost2(String url ,String openId,String time) {
        HttpClient client = HttpClients. createDefault();
        HttpPost post = new HttpPost(url);

        JSONObject json = new JSONObject();
        json.put("openId", openId);
        json.put("time", time);
        int StatusCode=0;
        String Response="";
        try {
            post.addHeader("Content-type", "application/json; charset=utf-8");
            post.setHeader("Accept", "application/json");
            post.setEntity(new StringEntity(json.toString(), Charset.forName("UTF-8")));
            HttpResponse httpResponse = client.execute(post);
            HttpEntity entity = httpResponse.getEntity();
            StatusCode=httpResponse.getStatusLine().getStatusCode();
            if(StatusCode==200){
                Response=EntityUtils.toString(entity);
                 System.out.println("status:" + httpResponse.getStatusLine());
                 System.out.println("response content:" + Response);
            }else{
                Response=String.valueOf(StatusCode);
            }
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Response;
    }
    public static void main(String[] args) {
        String url="http://baidu.com:80/getPostMessageCount";
        String openId="dhua";
        String time=new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String  responseJson=submitPost2(url,openId,time);
        System.out.println(responseJson);
        
    }
    
}

====================================================================

public class HttpUtil {
    private static Logger logger = LogManager.getLogger(HttpUtil.class);

    public static String httpPost(String url, String request) {
        String response = "";
        PostMethod postMethod = new PostMethod(url);
        // 然后把Soap请求数据添加到PostMethod中
        try {
            byte[] b = request.getBytes("utf-8");
            InputStream is = new ByteArrayInputStream(b, 0, b.length);
            RequestEntity re = new InputStreamRequestEntity(is, b.length,
                    "text/xml;charset=UTF-8");
            postMethod.setRequestEntity(re);
            // 最后生成一个HttpClient对象,并发出postMethod请求
            HttpClient httpClient = new HttpClient();
            // 执行这个web服务,并返回这个的状态
            int statusCode = httpClient.executeMethod(postMethod);
            if (statusCode == 200) {
                // 得到web服务返回的数据
                response = postMethod.getResponseBodyAsString();
                // 释放连接
                postMethod.releaseConnection();
            } else {
                System.out.println("http调用失败:code-" + statusCode);
            }
        } catch (Exception e) {
            logger.error("http调用失败:", e);
            //要抛出异常--到 ---调用该方法中---
        }
        return response;
    }
}

猜你喜欢

转载自blog.csdn.net/xiaoleizhanghahaha/article/details/86557870