Request的原生 http 请求的通讯信息资料

直接上 干货!!!

http原生调用代码如下:

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.StatusLine;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gome.meidian.restfulcommon.utils.JSONUtils;
import com.gome.meidian.vo.outputVo.PageInfo;

@RestController
public class AAA {
private static RequestConfig config = RequestConfig.copy(RequestConfig.DEFAULT).setSocketTimeout(5000).setConnectTimeout(3000).setConnectionRequestTimeout(2000).build();
//http请求
@SuppressWarnings({ "deprecation", "unchecked" })
public static String getHttp() throws Exception {
String url = "http://prom.mobile.atguat.com.cn/mobile/shop/fillGoodsExtProperty.jsp";
//body参数
Map<String, Object> mapHome = new HashMap<>();
mapHome.put("keyProms", "channelNgHO2PJZzNi5v");
String jsonString = JSONUtils.toJSONString(mapHome);
jsonString=URLEncoder.encode(jsonString);
String params = "body="+jsonString;

String ret = "";
String pageInfoString = "";
HttpGet get = new HttpGet(new StringBuilder().append(url).append("?").append(params).toString());
get.setConfig(config);
get.removeHeaders("User-Agent");
get.addHeader("User-Agent", "Android 0.1");
get.addHeader("Accept-Encoding", "gzip");
get.addHeader("Content-Type", "application/x-www-form-urlencoded");
get.addHeader(HTTP.CONTENT_ENCODING, "UTF-8");
CloseableHttpResponse response = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
response = httpclient.execute(get);
StatusLine status = response.getStatusLine();
if (status.getStatusCode() == 200) {
ret = EntityUtils.toString(response.getEntity(), "UTF-8");
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> responseMap = new HashMap<>();
responseMap = mapper.readValue(ret, Map.class);

PageInfo pageInfo = new PageInfo();
Object pageInfoObject = responseMap.get("pageInfo");
if(pageInfoObject != null){
String jsonStringaa = JSONUtils.toJSONString(pageInfoObject);
pageInfo = JSON.parseObject(jsonStringaa, PageInfo.class);
pageInfoString = pageInfo.toString();
}
}
} catch (Exception e) {
throw new Exception(e.getMessage(),e);
}  finally {
if (response != null) {
try {
httpclient.close();
response.close();
} catch (Exception e) {
}
}
}
return pageInfoString;
}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值