zoukankan      html  css  js  c++  java
  • HttpClient 通过代理访问验证服务器

    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());
            }
    }
  • 相关阅读:
    bash :startx command not found
    RHSA-2019:1880-低危: curl 安全和BUG修复更新 及 RHSA-2019:1884-中危: libssh2
    CentOS8 安装后 无法切换中文输入法
    【centOS7】centOS7上普通用户切换root用户,相互切换
    centos8安装图解(超详细教程)
    电导率与土壤盐浓度间的计算公式
    Tlink物联网平台使用(三)
    Wireshark查看https的通讯
    docker中建立私有git服务器[gitlab]
    【转】程序员的生产力始于需求而非工具
  • 原文地址:https://www.cnblogs.com/liuzhenlei/p/8333941.html
Copyright © 2011-2022 走看看