HttpClientDemo

public void init(){
    Map requestParam=new HashMap<>();
    requestParam.put("name","哈哈");
    requestParam.put("age", 18);

    String jsonRequestParam = JSON.toJSONString(requestParam);

    Map abc=new HashMap<>();
    abc.put("abc", jsonRequestParam);
    String json = JSON.toJSONString(requestParam);
    HttpClient httpClient= HttpClientBuilder.create().build();
    HttpPost httpPost=new HttpPost("http://127.0.0.1/asd");

    httpPost.addHeader("asdssd","123232323");
    httpPost.addHeader("Content-type","application/json;charset=utf-8");
    HttpEntity body=new StringEntity(json,"UTF-8");
    httpPost.setEntity(body);


    HttpResponse httpResponse=null;

    try {
        httpResponse=httpClient.execute(httpPost);
        HttpEntity entity=httpResponse.getEntity();
        if(entity!=null){
            InputStream in=entity.getContent();
            byte[] bs = ASD.ghj(in);

            String str=new String(bs,"utf-8");
            System.out.println(str);

        }
    } catch (Exception e) {
        e.printStackTrace();
    }


}

public static byte[] ghj(InputStream in) throws Exception{
    byte[] bs=null;
    ByteArrayOutputStream baos=new ByteArrayOutputStream();

    int b=0;
    b=in.read();
    while(b!=-1){
        baos.write(b);
        b=in.read();
    }

    bs=baos.toByteArray();
    return bs;
}

你可能感兴趣的:(java,开发语言)