java后台调用http_使用HttpClient 后台调用接口

scikitlearn tensorflow实用教材

71.1元

包邮

(需用券)

去购买 >

5ba567ca3302f5d321cf47349da47d69.png

//接口与对象的条件绑定并返回json数据

public static String executeHttpRequestByGetType(String url,Object obj ) {

HttpClient client = new HttpClient();

StringBuffer sb = new StringBuffer(url);

Map keyValueMap=objectToMap(obj);

PostMethod postMethod = null;

try {

//设置请求参数

if (keyValueMap != null) {

Iterator it = keyValueMap.entrySet().iterator();

if (keyValueMap.size() > 0) {

sb.append("?");

while (it.hasNext()) {

Map.Entry entry = (Map.Entry) it.next();

sb.append(entry.getKey() + "=" + entry.getValue() + "&");

}

sb.deleteCharAt(sb.length()-1);

}

}

postMethod = new PostMethod(sb.toString());

//log.debug("query uri ===============" + postMethod.getURI());

// 设置成了默认的恢复策略,在发生异常时候将自动重试3次,

postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());

//设置参数编码为gbk

postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");

//设置超时时间

postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 200000);

//HttpClient第一步中创建好的实例的 executeMethod 方法来执行第二步中创建好的 method 实例

int statusCode = client.executeMethod(postMethod);

if (statusCode != HttpStatus.SC_OK) {

//log.info("request '" + url + "' failed,the status is not 200,status:" + statusCode);

return "";

}

String responseBody = postMethod.getResponseBodyAsString();

return responseBody;

} catch (Exception e) {

//log.error("发生异常!请检查网络和参数", e);

} finally {

if(postMethod!=null){

postMethod.releaseConnection();

}

}

return null;

}

@Test

public void test(){

TestBean be=new TestBean();

InterfaceData da=new InterfaceData();

String url= "http://59.111.44.97:9096/webapi/TakeAll";//接口地址

Map msp=new HashMap();

String s=da.executeHttpRequestByGetType(url,be);

//JSONUtil.deserialize(s);

System.out.println(s);

}

//object转化为Map

public static Map objectToMap(Object obj){

Map map=null;

if(obj==null){

return map;

}else{

try {

map= new HashMap();

BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());

PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

for (PropertyDescriptor property : propertyDescriptors) {

String key = property.getName();

if (key.compareToIgnoreCase("class") == 0) {

continue;

}

Method getter = property.getReadMethod();

Object value = getter!=null ? getter.invoke(obj) : null;

if(value!=null){

map.put(key, value.toString());

}

}

return map;

} catch (IntrospectionException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalArgumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InvocationTargetException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}

}

原文链接:http://1069843121.iteye.com/blog/2401362

java 11官方入门(第8版)教材

79.84元

包邮

(需用券)

去购买 >

f0f3f55624fb396b1764d42d6df88864.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值