com.fufang.httprequest.HttpPostBuild.java Source code

Java tutorial

Introduction

Here is the source code for com.fufang.httprequest.HttpPostBuild.java

Source

package com.fufang.httprequest;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpPostBuild {

    public static String postBuildJson(String url, String params)
            throws UnsupportedEncodingException, ClientProtocolException, IOException {

        CloseableHttpClient httpClient = HttpClients.createDefault();
        String postResult = null;

        HttpPost httpPost = new HttpPost(url);
        System.out.println(" request " + httpPost.getRequestLine());

        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000)
                .build();
        httpPost.setConfig(requestConfig);
        StringEntity entity = new StringEntity(params.toString(), "UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);

        try {
            HttpResponse response = httpClient.execute(httpPost);
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                HttpEntity responseEntity = response.getEntity();
                postResult = EntityUtils.toString(responseEntity);
            } else {
                System.out.println("unexpected response status - " + status);
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return postResult;
    }

    /*public static void main(String[] args) throws ClientProtocolException, IOException{
       String url = "http://172.16.87.192:7777/ffcloud-epscm/pullMaterial/saveOrUpdateAll.do";
        
       Map<String, String> data = new HashMap<String, String>();
       data.put("barcode","????abc");
       data.put("matcode","???abc");
       data.put("batchNum","?qbc");
       data.put("name","??");
       data.put("commonName","??");
       data.put( "spec","");
       data.put("unit","??");
       data.put("dosage","");
       data.put("licenseNum","?");
       data.put("manufName","");
       data.put("prodPlace","?");
       data.put("productDate","2012-12-12");
       data.put("validDate","2013-12-13");
       data.put("retailPrice","12.5");
       data.put("zhongbao","10");
       data.put("createPerson","");
       data.put( "sellState","y");
       data.put("manufNameAbbr","");
       data.put("is_kcbxs","");
       data.put("price","12");
       data.put("storeNum","100");
        
       JSONArray dataArray = JSONArray.fromObject(data);
       JSONObject params = new JSONObject();
       params.put("key","61300");
       params.put("data",dataArray);
       System.out.println("? - " + params);
        
       HttpPostEP.postJson(url, params);
    }*/
}