前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >requestmethod post和get_post与get

requestmethod post和get_post与get

作者头像
全栈程序员站长
发布2022-09-27 14:41:21
1K0
发布2022-09-27 14:41:21
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

一、GetMethod

代码语言:javascript
复制
try {
                         
    HttpClient client = new HttpClient();
    String OrderId_url ="http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long="+req.getScheme()+"://"+ req.getServerName()+req.getContextPath()+"/wx/showWxOrderDetail.do?orderId="+thirdPayLogDetail.getSaleOrderSysId();
    GetMethod get = new GetMethod(OrderId_url);                            
     get.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
    int status = client.executeMethod(get);
    String res = get.getResponseBodyAsString().trim();
    System.out.println("返回回来的数据:"+res+ "  状态值:" + status);
    //返回回来的数据:[{"url_short":"http://t.cn/RONu69s","url_long":"http://dev7.89t.cn/ebusiness/wx/showWxOrderDetail.do?orderId=12451","type":0}]  状态值:200                      
    JSONArray result=JSONArray.fromObject(res);//转json对象
    JSONObject getJsonObj = result.getJSONObject(0);
    int type = (int) getJsonObj.get("type");
    String url_short = getJsonObj.getString("url_short");
    System.out.println("返回状态:"+type+"=======短接口:"+url_short);

二、PostMethod

代码语言:javascript
复制
HttpClient client = new HttpClient();
String methodName="temporaryOrder.do";
String serverUrl=
PostMethod post = new PostMethod(serverUrl);
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");     
try {
    int status = client.executeMethod(post);
    String response = post.getResponseBodyAsString().trim();
    System.out.println("返回回来的数据:"+response);
    if (logger.isInfoEnabled()) {
        logger.info("返回信息:" + response + "  状态值:" + status);
    }
    JSONObject result=JSONObject.fromObject(response);//转json对象
    JSONObject headOb=result.getJSONObject("head");//头部信息
    JSONObject resultBody=result.getJSONObject("body");
    if (response.contains("\"code\":\"200\"")) {//请求成功
        //TODO 记录订单明细号
        if (resultBody.has("orderDetail")) {
 
        }
        return true;
    }else{
        throw new BusinessException(headOb.getString("msg"));
    }
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/193294.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档