首页 新闻 会员 周边 捐助

这里的每一句都是什么意思

0
悬赏园豆:15 [已解决问题] 解决于 2015-09-21 11:38

public static void main(String[] args) throws Exception {
        BasicCookieStore cookieStore = new BasicCookieStore();
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCookieStore(cookieStore)
                .build();
        try {
            HttpGet httpget = new HttpGet("https://someportal/");
            CloseableHttpResponse response1 = httpclient.execute(httpget);
            try {
                HttpEntity entity = response1.getEntity();

                System.out.println("Login form get: " + response1.getStatusLine());
                EntityUtils.consume(entity);

                System.out.println("Initial set of cookies:");
                List<Cookie> cookies = cookieStore.getCookies();
                if (cookies.isEmpty()) {
                    System.out.println("None");
                } else {
                    for (int i = 0; i < cookies.size(); i++) {
                        System.out.println("- " + cookies.get(i).toString());
                    }
                }
            } finally {
                response1.close();
            }

            HttpUriRequest login = RequestBuilder.post()
                    .setUri(new URI("https://someportal/"))
                    .addParameter("IDToken1", "username")
                    .addParameter("IDToken2", "password")
                    .build();
            CloseableHttpResponse response2 = httpclient.execute(login);
            try {
                HttpEntity entity = response2.getEntity();

                System.out.println("Login form get: " + response2.getStatusLine());
                EntityUtils.consume(entity);

                System.out.println("Post logon cookies:");
                List<Cookie> cookies = cookieStore.getCookies();
                if (cookies.isEmpty()) {
                    System.out.println("None");
                } else {
                    for (int i = 0; i < cookies.size(); i++) {
                        System.out.println("- " + cookies.get(i).toString());
                    }
                }
            } finally {
                response2.close();
            }
        } finally {
            httpclient.close();
        }
    }

狗霸人间的主页 狗霸人间 | 初学一级 | 园豆:4
提问于:2015-09-21 10:58
< >
分享
最佳答案
0

// 使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。
// 1. 创建HttpClient对象。
// 2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
// 3. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。
// 4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
// 5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
// 6. 释放连接。无论执行方法是否成功,都必须释放连接
//入口函数
public static void main(String[] args) throws Exception {
        //cookie存储类
        BasicCookieStore cookieStore = new BasicCookieStore();
        //创建一个httpclient,并设置默认cookie存储cookieStore
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCookieStore(cookieStore)
                .build();
        try {
            //创建HttpGet对象,需要发送GET请求https://someportal/
            HttpGet httpget = new HttpGet("https://someportal/");
            //HttpClient对象的execute(HttpUriRequest request)发送请求
            CloseableHttpResponse response1 = httpclient.execute(httpget);
            //try开始,试图执行
            try {
                //获取服务器返回的HttpEntity对象
                HttpEntity entity = response1.getEntity();
                //打印返回信息,response.getStatusLine().getStatusCode()==200链接成功,==500失败。
                System.out.println("Login form get: " + response1.getStatusLine());
                //HttpEntity对象提供的静态帮助类,EntityUtils.consume关闭资源
                EntityUtils.consume(entity);
                //打印Initial set of cookies:
                System.out.println("Initial set of cookies:");
                //获取Cookies并循环,空的输出None,否则输出cookie
                List<Cookie> cookies = cookieStore.getCookies();
                if (cookies.isEmpty()) {
                    System.out.println("None");
                } else {
                    for (int i = 0; i < cookies.size(); i++) {
                        System.out.println("- " + cookies.get(i).toString());
                    }
                }
            } finally {
                //关闭
                response1.close();
            }
            //POST请求,并设置URI和参数
            HttpUriRequest login = RequestBuilder.post()
                    .setUri(new URI("https://someportal/"))
                    .addParameter("IDToken1", "username")
                    .addParameter("IDToken2", "password")
                    .build();
            //执行POST请求
            CloseableHttpResponse response2 = httpclient.execute(login);
            try {
                //获取实体对象
                HttpEntity entity = response2.getEntity();
                //打印服务器返回信息
                System.out.println("Login form get: " + response2.getStatusLine());
                //关闭流
                EntityUtils.consume(entity);
                //打印cookie
                System.out.println("Post logon cookies:");
                List<Cookie> cookies = cookieStore.getCookies();
                if (cookies.isEmpty()) {
                    System.out.println("None");
                } else {
                    for (int i = 0; i < cookies.size(); i++) {
                        System.out.println("- " + cookies.get(i).toString());
                    }
                }
            } finally {
                response2.close();
            }
        } finally {
            //关闭httpclient
            httpclient.close();
        }
    }

收获园豆:15
JackWang-CUMT | 老鸟四级 |园豆:2866 | 2015-09-21 11:21

谢谢,说的很详细

狗霸人间 | 园豆:4 (初学一级) | 2015-09-21 11:36

@狗霸人间: 谢谢

JackWang-CUMT | 园豆:2866 (老鸟四级) | 2015-09-21 11:43
其他回答(2)
0

。。。。。。

✎﹏ℳ๓₯㎕ღ | 园豆:1499 (小虾三级) | 2015-09-21 11:07
0

public static void main(String [] args){抛出异常
basiccookiestore =新basiccookiestore()存储机制;
closeablehttpclient HttpClient = httpclients custom()。
setdefaultcookiestore(存储机制)。
build();
尝试{
HttpGet HttpGet =新HttpGet(“https:/ / someportal /”);
closeablehttpresponse response1 = HttpClient执行(HTTPGET);
尝试{
HttpEntity实体= response1 getentity();
系统。了。println(“登录形式得到:“+ response1 getstatusline()。);
entityutils消耗(实体);
系统。了。println(“初始设定的饼干:“);
列表<饼干>饼干= getcookies()存储机制;
如果(饼干。isempty()){
系统。了println(“无”);
{ }
为(int i = 0;i <饼干。size();i++){
系统。了。println(“-”+饼干。让(我)。tostring());
}
}
最后{
response1();
}
httpurirequest登录= requestbuilder post()。
seturi。(新的URI(“http:/ / someportal /”))
addparameter。(“idtoken1”,“用户名”)
addparameter。(“idtoken2”,“密码”)
build();
closeablehttpresponse反应= HttpClient执行(登录);
尝试{
HttpEntity实体=反应。getentity();
系统。了。println(“登录形式得到:“+反应。getstatusline());
entityutils消耗(实体);
系统。了。println(“后登录饼干:”);
列表<饼干>饼干= getcookies()存储机制;
如果(饼干。isempty()){
系统。了println(“无”);
{ }
为(int i = 0;i <饼干。size();i++){
系统。了。println(“-”+饼干。让(我)。tostring());
}
}
最后{
反应。();
}
最后{
HttpClient();
}
}

大楚打码人 | 园豆:4313 (老鸟四级) | 2015-09-21 11:17

你这个百度翻译啊。。。。。

支持(0) 反对(0) 狗霸人间 | 园豆:4 (初学一级) | 2015-09-21 11:32

@狗霸人间: 嗯嗯!

支持(0) 反对(0) 大楚打码人 | 园豆:4313 (老鸟四级) | 2015-09-21 12:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册