package com.olm.del; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; /** * ClassName:TokenTest1 Package:com.olm.util Date:2020/7/27 Auth:penghui@olm.com.cn */ public class TokenTest1 { public static void main(String[] args) { try { new TokenTest1().getToken("https://api.xiaoshouyi.com/oauth2/token"); } catch (Exception e) { e.printStackTrace(); } } public String getToken(String postURL) throws Exception { PostMethod postMethod = new PostMethod(postURL); postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); //参数设置,需要注意的就是里边不能传NULL,要传空字符串 NameValuePair[] data = { new NameValuePair("grant_type", "password"), new NameValuePair("Client_Id", "c97fbd8aca50ba94068da6ded23905df"), new NameValuePair("Client_Secret", "7a4d178ebf1d55229fd28c5cb18703da"), new NameValuePair("Redirect_Uri", "https://api.xiaoshouyi.com"), new NameValuePair("userName", "lyp@olm.com.cn"), new NameValuePair("password", "lyp123456") }; postMethod.setRequestBody(data); org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient(); httpClient.executeMethod(postMethod); /* String result = postMethod.getResponseBodyAsString(); System.out.println(result);*/ InputStream inputStream = postMethod.getResponseBodyAsStream(); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); StringBuffer stringBuffer = new StringBuffer(); String str = ""; while ((str = br.readLine()) != null) { stringBuffer.append(str); } System.out.println(stringBuffer); return null; } }