package com.customcode.util;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import weaver.general.BaseBean;
/**
* @author Yuyb
* @version 2020年3月27日 下午4:33:20
* 类说明
*/
public class HttpUtil_urlencoded {
public String dohttp(String postURL){
BaseBean baseBean = new BaseBean();
try {
PostMethod postMethod = null;
postMethod = new PostMethod(postURL) ;
postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;
//参数设置,需要注意的就是里边不能传NULL,要传空字符串
NameValuePair[] data = {
new NameValuePair("grant_type","client_credentials"),
new NameValuePair("client_id","srm-interface-client"),
new NameValuePair("client_secret","secretpro"),
//new NameValuePair("username",baseBean.getPropValue("SRMlinks", "tokenName")),
//new NameValuePair("password",baseBean.getPropValue("SRMlinks", "tokenPassword")),
new NameValuePair("scope","default")
//new NameValuePair("publicKey","")
};
postMethod.setRequestBody(data);
org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
int response = httpClient.executeMethod(postMethod); // 执行POST方法
String result = postMethod.getResponseBodyAsString() ;
//System.out.println(response);
//System.out.println(result);
return result;
} catch (Exception e) {
// logger.info("请求异常"+e.getMessage(),e);
throw new RuntimeException(e.getMessage());
}
}
}
|