HTTP协议post请求返回json数据

  1. 有两种方式:大家详细看看,非常好用!

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.jingyou.application.common.JSONUtils;
public class HttpClientCheHuiTDH {
    public static String doPost(Map map)throws Exception {
		//1 获得HttpClient这个工具类
    	HttpClient client = HttpClients.createDefault();
    	//2 设置Http请求方式 (get,post)
    	HttpPost post = new HttpPost("http://36.110.41.67:8090/dwjk/api/getAjbsAndAh/getNAjbs");//getAh
    	post.setHeader("systemid", "jingyoutimes");
    	post.setHeader("authcode", "3699185f24ddaa775805d5c476701098");
    	//3 传递数据
    //	JSONObject response = null;
    	String jsonTDH = JSONUtils.bean2json(map);
    	System.out.println(jsonTDH);
        try {
          StringEntity s = new StringEntity(jsonTDH.toString());
          s.setContentEncoding("UTF-8");
          s.setContentType("application/json");//发送json数据需要设置contentType
          post.setEntity(s);
          HttpResponse res = client.execute(post);
          if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
            HttpEntity entity = res.getEntity();
            String result = EntityUtils.toString(res.getEntity());// 返回json格式:
           // response = JSONObject.fromObject(result);
            System.out.println(result);
            return result;
          }
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
        return null;
    	
    	//NameValuePair  NJbfyPair = new BasicNameValuePair("NJbfy","2050");
    /*	NameValuePair  NJbfyPair = new BasicNameValuePair("NJbfy","1700");
    	List<NameValuePair> params = new ArrayList<NameValuePair>();
    	params.add(NJbfyPair);
    	HttpEntity sendEntity = new UrlEncodedFormEntity(params,"UTF-8");
    	post.setEntity(sendEntity);
    	//4 发送请求  并 接受响应
    	HttpResponse response = client.execute(post);
    	
    	 * HttpClient中 把请求 或 响应的数据 都称之为 HttpEntity;
    	 
    	HttpEntity entity = response.getEntity();
    	InputStream is = entity.getContent();
    	//开始 传统的IO操作
    	InputStreamReader isr =  new InputStreamReader(is,"UTF-8");
    	BufferedReader br  = new BufferedReader(isr);
    	String flag = null;
    	while(true){
    		flag = br.readLine();
    		if(flag==null)break;
    		System.out.println(flag);
    		return flag;
    	}
    	return flag;*/
	}
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寅灯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值