public static void main(String[] args) {
String urle="http://192.168.1.107:12811/oauth/check_token";
String token="RlNzMtODRmNC0yMTGrBQVIBhh435qCYveTOQV6OQwyyCnADUIKQn_yQ";
String s = doPost( urle, token );
System.out.println(s);
}
public static String doPost(String url,String token){
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(3000);
client.getHttpConnectionManager().getParams().setSoTimeout(3000);
PostMethod method = new UPostMethod(url);//UPostMethod继承PostMethod,见下所示
String result = "";
try {
method.addParameter("token",token);
client.executeMethod(method);
byte[] response = method.getResponseBody();
result = new String(response,"UTF-8");//返回值解析时用的编码格式
} catch (Exception e) {
throw new RuntimeException("创建连接失败" + e);
}finally{
method.releaseConnection();
}
return result;
}
设置发送参数的编码格式
package com.bfdb.untils;
import org.apache.commons.httpclient.methods.PostMethod;
public class UPostMethod extends PostMethod {
public UPostMethod(String url){
super(url);
}
@Override
public String getRequestCharSet(){
return "UTF-8";//设置发送参数的编码格式
}
}
需要的jar
<!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>