利用 Java 实现接口 POST 请求测试

下面这个postDemo类是java的post请求去调用http://localhost:8080/callBack的接口

public class postDemo {

         private static HttpClient httpClient = new DefaultHttpClient();
         private static HttpPost httppost;
         private static HttpResponse response;
         private HttpEntity entity;
         private String postResult = null;

         public static void main(String[] args) {
 

                String loginURL = "http://localhost:8080/callBack";
                // 创建一个httppost请求
                httppost = new HttpPost(loginURL);
                JSONObject jsonParam = new JSONObject();
                jsonParam.put("Origin_File", Origin_File);
                jsonParam.put("Batch_Status",Batch_Status);
                jsonParam.put("File_List",File_List);
                try {

                    StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");// 解决中文乱码问题
                    entity.setContentEncoding("UTF-8");
                    entity.setContentType("application/json");
                    httppost.setEntity(entity);
                    response = httpClient.execute(httppost); 
                    String strResult = EntityUtils.toString(response.getEntity());
                    System.out.println("查看返回的结果:" + strResult);


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

                httppost.releaseConnection();

     }

}

 

下面这个是被调用的接口

@RestController
public class PhoneStatusController {
     private static final Logger logger = LoggerFactory.getLogger(PhoneStatusController.class);
    @RequestMapping("/hello1")
    public String test() {
        return "hello,this is a springboot demo";
    }

    @RequestMapping(value = "/callBack", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject dxCallback(
            HttpServletRequest request, HttpServletResponse response) throws IOException {
        String content = "";
        try {
            content = IOUtils.toString(request.getInputStream(), "UTF-8");
        } catch (IOException e) {
            logger.info("failure! Exception: {}", e);
        }
        logger.info("Notify content: {}", content);
        JSONObject json = JSON.parseObject(content);
        //{"desc":"正常","phone":"13790719324","reqId":"yMrBmOAvnV4j5gXz9PVYj5","status":"0000"}
        String Origin_File = json.getString("Origin_File");
        String Process_Status = json.getString("Batch_Status");
       
        logger.info("Origin_File: {}, Process_Status: {}", Origin_File, Process_Status);
       
        JSONObject reqData = new JSONObject();
        reqData.put("Origin_File", Origin_File);
        reqData.put("Process_Status", 1);
        

        return reqData;
    }
}

最后返回结果是{"Process_Status":1,"Origin_File":"WDBR_SpecialList_20190315.dat"}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值