import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.PostMethod; import com.alibaba.fastjson.JSONObject; public class test { public static void main(String[] args) { //穿件httpClient对象 HttpClient client = new HttpClient(); //配置代理服务器ip、端口 client.getHostConfiguration().setProxy("183.30.201.144",9797); client.getParams().setAuthenticationPreemptive(true); //设置代理服务器账户密码 // client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials("anyo", "Pass1234")); //设置目标服务器验证的账户密码 Credentials aa = new UsernamePasswordCredentials("roewe_query", "1234567890abcdef"); client.getState().setCredentials(AuthScope.ANY,aa); //目标URL,配置post方法 PostMethod postMethod = new PostMethod("https://myapi.anyocharging.com:18765/auth/oauth/token?grant_type=client_credentials"); // GetMethod postMethod = new GetMethod("http://www.mi.com"); StringBuffer response=new StringBuffer(); try { client.executeMethod(postMethod); BufferedReader reader; System.out.println(postMethod.getStatusCode()); reader = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream(),"UTF-8")); String line; while ((line = reader.readLine()) != null) { System.out.println(line); response.append(line); } reader.close(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } JSONObject jsonObject = JSONObject.parseObject(response.toString()); System.out.println(jsonObject.toJSONString()); } }
liuzhenlei的学习博客