`
DavidIsOK
  • 浏览: 75707 次
社区版块
存档分类
最新评论

httpclient 实例(转)

    博客分类:
  • java
阅读更多

新版HttpClient4.2与之前的3.x版本有了很大变化,建议从http://hc.apache.org/处以得到最新的信息。

  1. 关于HttpCore与HttpClient:HttpCore是位于HTTP传输组件的底层包,可以用来简化HTTP客户端与服务器端的开发。HttpClient是一个符合HTTP1.1版本,基于HttpCore类包的一个实现。它同时为客户端认证、HTTP状态管理、HTTP连接管理提供了可重用的客户端组件。HttpCore类包目前最新发布版本是httpcore-4.2.4;HttpClient类包的版本是httpclient-4.2.5。
    了解到HttpCore包与HttpClient包的差别,在程序中就应该大致知道一些包它们存在于哪个类库中。比如:org.apache.http包属于HttpCore,而org.apache.http.client包属于HttpClient。
  2. HttpClient的API文档在下载的zip中已经包括;
    HttpCore的API文档可以参考:http://hc.apache.org/httpcomponents-core-4.2.x/httpcore/apidocs/index.html
  3. HttpClient4.2需要Java 5.0及以上版本;需要支持包有(下载zip包中已经包括):
    * Apache HttpComponents HttpCore
    * Apache Commons Logging
    * Apache Commons Codec

1. 获取一个HTML页面的内容,一个简单的get应用

复制代码
    // 获取一个HTML页面的内容,一个简单的get应用
    public void grabPageHTML() throws Exception {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.baidu.com/");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String html = EntityUtils.toString(entity, "GBK");
        
        // releaseConnection等同于reset,作用是重置request状态位,为下次使用做好准备。
        // 其实就是用一个HttpGet获取多个页面的情况下有效果;否则可以忽略此方法。
        httpget.releaseConnection();

        System.out.println(html);
    }
复制代码

2. 下载一个文件到本地(本示范中为一个验证码图片)

复制代码
    // 下载一个文件到本地(本示范中为一个验证码图片)
    public void downloadFile() throws Exception {
        String url = "http://www.lashou.com/account/captcha";
        String destfilename = "D:\\TDDOWNLOAD\\yz.png";
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        File file = new File(destfilename);
        if (file.exists()) {
            file.delete();
        }
        
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        InputStream in = entity.getContent();
        try {
            FileOutputStream fout = new FileOutputStream(file);
            int l = -1;
            byte[] tmp = new byte[2048]; 
            while ((l = in.read(tmp)) != -1) {
                fout.write(tmp);
            } 
            fout.close();
        } finally {
            // 在用InputStream处理HttpEntity时,切记要关闭低层流。
            in.close();
        }
        
        httpget.releaseConnection();
    }
复制代码

3. Post方法,模拟表单提交参数登录到网站并打开会员页面获取内容(会话保持)

复制代码
    // Post方法,模拟表单提交参数登录到网站。
    // 结合了上面两个方法:grabPageHTML/downloadFile,同时增加了Post的代码。
    public void login2Lashou() throws Exception {
        // 第一步:先下载验证码到本地
        String url = "http://www.lashou.com/account/captcha";
        String destfilename = "D:\\TDDOWNLOAD\\yz.png";
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        File file = new File(destfilename);
        if (file.exists()) {
            file.delete();
        }
        
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        InputStream in = entity.getContent();
        try {
            FileOutputStream fout = new FileOutputStream(file);
            int l = -1;
            byte[] tmp = new byte[2048]; 
            while ((l = in.read(tmp)) != -1) {
                fout.write(tmp);
            } 
            fout.close();
        } finally {
            in.close();
        }
        httpget.releaseConnection();

// 第二步:用Post方法带若干参数尝试登录,需要手工输入下载验证码中显示的字母、数字 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入下载下来的验证码中显示的数字..."); String yan = br.readLine(); HttpPost httppost = new HttpPost("http://www.lashou.com/account/login/"); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("user_id", "testuser007")); params.add(new BasicNameValuePair("pwd", "asdfg123")); params.add(new BasicNameValuePair("yan", yan)); params.add(new BasicNameValuePair("save_user", "on")); params.add(new BasicNameValuePair("save_pwd", "on")); params.add(new BasicNameValuePair("sub", "登录")); httppost.setEntity(new UrlEncodedFormEntity(params)); response = httpclient.execute(httppost); entity = response.getEntity(); // 在这里可以用Jsoup之类的工具对返回结果进行分析,以判断登录是否成功 String postResult = EntityUtils.toString(entity, "GBK"); // 我们这里只是简单的打印出当前Cookie值以判断登录是否成功。 List<Cookie> cookies = ((AbstractHttpClient)httpclient).getCookieStore().getCookies(); for(Cookie cookie: cookies) System.out.println(cookie); httppost.releaseConnection();
// 第三步:打开会员页面以判断登录成功(未登录用户是打不开会员页面的) String memberpage = "http://www.lashou.com/account/orders/"; httpget = new HttpGet(memberpage); response = httpclient.execute(httpget); // 必须是同一个HttpClient! entity = response.getEntity(); String html = EntityUtils.toString(entity, "GBK"); httpget.releaseConnection(); System.out.println(html); }
复制代码

输出:

请输入下载下来的验证码中显示的数字...
sbzq
...

[version: 0][name: login_name2][value: testuser007][domain: www.lashou.com][path: /][expiry: Mon Sep 09 10:21:19 CST 2013]
[version: 0][name: pwd2][value: 4c88a4062736c26572d3ec382868fa2b][domain: lashou.com][path: /][expiry: Mon Sep 09 10:21:19 CST 2013]
?<!doctype html>

...

4. 设置代理服务器

复制代码
    // 设置代理服务器
    public void testProxy() throws Exception {
        HttpHost proxy = new HttpHost("127.0.0.1", 8888);
        
        // 方式一
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        
        // 方式二
        HttpParams params = new BasicHttpParams(); 
        params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        HttpClient httpclient1 = new DefaultHttpClient(params);
    }
复制代码

5. 几种常用HTTP头的设置

复制代码
    // 几种常用HTTP头的设置
    public void testBasicHeader() throws Exception {
        HttpParams params = new BasicHttpParams(); 
        Collection<BasicHeader> collection = new ArrayList<BasicHeader>();
        collection.add(new BasicHeader("Accept", "text/html, application/xhtml+xml, */*"));
        collection.add(new BasicHeader("Referer", "http://www.sina.com/"));
        collection.add(new BasicHeader("Accept-Language", "zh-CN"));
        collection.add(new BasicHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"));
        collection.add(new BasicHeader("Accept-Encoding", "gzip, deflate"));
        params.setParameter(ClientPNames.DEFAULT_HEADERS, collection);
        
        HttpClient httpclient = new DefaultHttpClient(params);
        
        // 下面内容略
    }
复制代码

6. 多线程编程下的线程池设置

复制代码
    // 多线程编程下的线程池设置(这点在需要登录且用一个HttpClient对象抓取多个页面的情况下特别有用)
    public void testConnectionManager() throws Exception {
        // 连接池设置
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

        PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
        cm.setMaxTotal(200); // 连接池里的最大连接数
        cm.setDefaultMaxPerRoute(20); // 每个路由的默认最大连接数
        HttpHost localhost = new HttpHost("locahost", 80); // 可以针对某特定网站指定最大连接数
        cm.setMaxPerRoute(new HttpRoute(localhost), 30);

        // 其它设置
        HttpParams params = new BasicHttpParams(); 
        params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
        HttpClient httpclient = new DefaultHttpClient(cm, params);
        
        // 下面内容略
    }
复制代码

7. 测试HTTP上下文对象(HttpContext)

复制代码
    // 测试HTTP上下文对象(HttpContext)
    public void testContext() throws Exception {
        // 请求一个页面,然后解析各上下文对象
        DefaultHttpClient httpclient = new DefaultHttpClient();

        HttpContext localContext = new BasicHttpContext();
        HttpGet httpget = new HttpGet("http://www.baidu.com/");

        HttpResponse response = httpclient.execute(httpget, localContext);
        
        // the actual connection to the target server.
        HttpConnection conn = (HttpConnection) localContext.getAttribute(
            ExecutionContext.HTTP_CONNECTION);  
        System.out.println("Socket timeout: " + conn.getSocketTimeout());  

        // the connection target
        HttpHost target = (HttpHost) localContext.getAttribute(
            ExecutionContext.HTTP_TARGET_HOST);
        System.out.println("Final target: " + target);
        
        // the connection proxy, if used 
        HttpHost proxy = (HttpHost) localContext
                .getAttribute(ExecutionContext.HTTP_PROXY_HOST);
        if (proxy != null)
            System.out.println("Proxy host/port: " + proxy.getHostName() + "/"
                    + proxy.getPort());

        // the actual HTTP request
        HttpRequest request = (HttpRequest) localContext
                .getAttribute(ExecutionContext.HTTP_REQUEST);
        System.out.println("HTTP version: " + request.getProtocolVersion());
        Header[] headers = request.getAllHeaders();
        System.out.println("HTTP Headers: ");
        for (Header header : headers) {
            System.out.println("\t" + header.getName() + ": " + header.getValue());
        }
        System.out.println("HTTP URI: " + request.getRequestLine().getUri());

        // the actual HTTP response
        response = (HttpResponse) localContext
                .getAttribute(ExecutionContext.HTTP_RESPONSE);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            System.out.println("Content Encoding:" + entity.getContentEncoding());
            System.out.println("Content Type:" + entity.getContentType());
        }
        
        // the flag indicating whether the actual request has been fully transmitted to the connection target. 
        System.out.println("Sent flag: " + localContext.getAttribute(ExecutionContext.HTTP_REQ_SENT));

// 如果没有用到返回entity中的内容,那么要把它消费掉,以保证底层的资源得以释放。 entity = response.getEntity(); EntityUtils.consume(entity); }
复制代码

输出:

Socket timeout: 0
Final target: http://www.baidu.com
HTTP version: HTTP/1.1
HTTP Headers: 
    Host: www.baidu.com
    Connection: Keep-Alive
    User-Agent: Apache-HttpClient/4.2.5 (java 1.5)
HTTP URI: /
Content Encoding:null
Content Type:Content-Type: text/html;charset=utf-8
Sent flag: true

8. 完整的代码

复制代码
package com.clzhang.sample.net;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

public class HttpClientSample1 {
    
    // 获取一个HTML页面的内容,一个简单的get应用
    public void grabPageHTML() throws Exception {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.baidu.com/");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String html = EntityUtils.toString(entity, "GBK");
        
        // releaseConnection等同于reset,作用是重置request状态位,为下次使用做好准备。
        // 其实就是用一个HttpGet获取多个页面的情况下有效果;否则可以忽略此方法。
        httpget.releaseConnection();

        System.out.println(html);
    }
    
    // 下载一个文件到本地(本示范中为一个验证码图片)
    public void downloadFile() throws Exception {
        String url = "http://www.lashou.com/account/captcha";
        String destfilename = "D:\\TDDOWNLOAD\\yz.png";
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        File file = new File(destfilename);
        if (file.exists()) {
            file.delete();
        }
        
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        InputStream in = entity.getContent();
        try {
            FileOutputStream fout = new FileOutputStream(file);
            int l = -1;
            byte[] tmp = new byte[2048]; 
            while ((l = in.read(tmp)) != -1) {
                fout.write(tmp);
            } 
            fout.close();
        } finally {
            // 在用InputStream处理HttpEntity时,切记要关闭低层流。
            in.close();
        }
        
        httpget.releaseConnection();
    }
    
    // Post方法,模拟表单提交参数登录到网站。
    // 结合了上面两个方法:grabPageHTML/downloadFile,同时增加了Post的代码。
    public void login2Lashou() throws Exception {
        // 第一步:先下载验证码到本地
        String url = "http://www.lashou.com/account/captcha";
        String destfilename = "D:\\TDDOWNLOAD\\yz.png";
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        File file = new File(destfilename);
        if (file.exists()) {
            file.delete();
        }
        
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        InputStream in = entity.getContent();
        try {
            FileOutputStream fout = new FileOutputStream(file);
            int l = -1;
            byte[] tmp = new byte[2048]; 
            while ((l = in.read(tmp)) != -1) {
                fout.write(tmp);
            } 
            fout.close();
        } finally {
            in.close();
        }
        httpget.releaseConnection();
        
        // 第二步:用Post方法带若干参数尝试登录,需要手工输入下载验证码中显示的字母、数字
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("请输入下载下来的验证码中显示的数字...");
        String yan = br.readLine();

        HttpPost httppost = new HttpPost("http://www.lashou.com/account/login/");
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("user_id", "testuser007"));
        params.add(new BasicNameValuePair("pwd", "asdfg123"));
        params.add(new BasicNameValuePair("yan", yan));
        params.add(new BasicNameValuePair("save_user", "on"));
        params.add(new BasicNameValuePair("save_pwd", "on"));
        params.add(new BasicNameValuePair("sub", "登录"));
        httppost.setEntity(new UrlEncodedFormEntity(params));
        
        response = httpclient.execute(httppost);
        entity = response.getEntity();
        // 在这里可以用Jsoup之类的工具对返回结果进行分析,以判断登录是否成功
        String postResult = EntityUtils.toString(entity, "GBK"); 
        // 我们这里只是简单的打印出当前Cookie值以判断登录是否成功。
        List<Cookie> cookies = ((AbstractHttpClient)httpclient).getCookieStore().getCookies();
        for(Cookie cookie: cookies)
            System.out.println(cookie);
        httppost.releaseConnection();
        
        // 第三步:打开会员页面以判断登录成功(未登录用户是打不开会员页面的)
        String memberpage = "http://www.lashou.com/account/orders/";
        httpget = new HttpGet(memberpage);
        response = httpclient.execute(httpget); // 必须是同一个HttpClient!
        entity = response.getEntity();
        String html = EntityUtils.toString(entity, "GBK");
        httpget.releaseConnection();

        System.out.println(html);
    }
    
    // 设置代理服务器
    public void testProxy() throws Exception {
        HttpHost proxy = new HttpHost("127.0.0.1", 8888);
        
        // 方式一
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        
        // 方式二
        HttpParams params = new BasicHttpParams(); 
        params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        HttpClient httpclient1 = new DefaultHttpClient(params);
    }
    
    // 几种常用HTTP头的设置
    public void testBasicHeader() throws Exception {
        HttpParams params = new BasicHttpParams(); 
        Collection<BasicHeader> collection = new ArrayList<BasicHeader>();
        collection.add(new BasicHeader("Accept", "text/html, application/xhtml+xml, */*"));
        collection.add(new BasicHeader("Referer", "http://www.sina.com/"));
        collection.add(new BasicHeader("Accept-Language", "zh-CN"));
        collection.add(new BasicHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"));
        collection.add(new BasicHeader("Accept-Encoding", "gzip, deflate"));
        params.setParameter(ClientPNames.DEFAULT_HEADERS, collection);
        
        HttpClient httpclient = new DefaultHttpClient(params);
        
        // 下面内容略
    }
    
    // 多线程编程下的线程池设置(这点在需要登录且用一个HttpClient对象抓取多个页面的情况下特别有用)
    public void testConnectionManager() throws Exception {
        // 连接池设置
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

        PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
        cm.setMaxTotal(200); // 连接池里的最大连接数
        cm.setDefaultMaxPerRoute(20); // 每个路由的默认最大连接数
        HttpHost localhost = new HttpHost("locahost", 80); // 可以针对某特定网站指定最大连接数
        cm.setMaxPerRoute(new HttpRoute(localhost), 30);

        // 其它设置
        HttpParams params = new BasicHttpParams(); 
        params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
        HttpClient httpclient = new DefaultHttpClient(cm, params);
        
        // 下面内容略
    }
    
    // 测试HTTP上下文对象(HttpContext)
    public void testContext() throws Exception {
        // 请求一个页面,然后解析各上下文对象
        DefaultHttpClient httpclient = new DefaultHttpClient();

        HttpContext localContext = new BasicHttpContext();
        HttpGet httpget = new HttpGet("http://www.baidu.com/");

        HttpResponse response = httpclient.execute(httpget, localContext);
        
        // the actual connection to the target server.
        HttpConnection conn = (HttpConnection) localContext.getAttribute(
            ExecutionContext.HTTP_CONNECTION);  
        System.out.println("Socket timeout: " + conn.getSocketTimeout());  

        // the connection target
        HttpHost target = (HttpHost) localContext.getAttribute(
            ExecutionContext.HTTP_TARGET_HOST);
        System.out.println("Final target: " + target);
        
        // the connection proxy, if used 
        HttpHost proxy = (HttpHost) localContext
                .getAttribute(ExecutionContext.HTTP_PROXY_HOST);
        if (proxy != null)
            System.out.println("Proxy host/port: " + proxy.getHostName() + "/"
                    + proxy.getPort());

        // the actual HTTP request
        HttpRequest request = (HttpRequest) localContext
                .getAttribute(ExecutionContext.HTTP_REQUEST);
        System.out.println("HTTP version: " + request.getProtocolVersion());
        Header[] headers = request.getAllHeaders();
        System.out.println("HTTP Headers: ");
        for (Header header : headers) {
            System.out.println("\t" + header.getName() + ": " + header.getValue());
        }
        System.out.println("HTTP URI: " + request.getRequestLine().getUri());

        // the actual HTTP response
        response = (HttpResponse) localContext
                .getAttribute(ExecutionContext.HTTP_RESPONSE);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            System.out.println("Content Encoding:" + entity.getContentEncoding());
            System.out.println("Content Type:" + entity.getContentType());
        }
        
        // the flag indicating whether the actual request has been fully transmitted to the connection target. 
        System.out.println("Sent flag: " + localContext.getAttribute(ExecutionContext.HTTP_REQ_SENT));
        
        // 如果没有用到返回entity中的内容,那么要把它消费掉,以保证底层的资源得以释放。
        entity = response.getEntity();
        EntityUtils.consume(entity);
    }    
    
    public static void main(String[] args) throws Exception {
        HttpClientSample1 ins = new HttpClientSample1();
        
//        ins.grabPageHTML();
//        ins.downloadFile();
        ins.login2Lashou();
//        ins.testContext();
    }
}
复制代码
 
 
转自:http://www.cnblogs.com/nayitian/archive/2013/08/26/3282029.html
分享到:
评论

相关推荐

    httpClient实例

    自己做的httpClient实例,写的比较多刚接触的可以看看

    httpclient的入门实例

    1. **创建HttpClient实例**:使用`HttpClientBuilder`或`HttpClients`静态工厂方法创建一个HttpClient实例。 ```java HttpClient httpClient = HttpClients.createDefault(); ``` 2. **构建HttpRequest**:...

    httpClient实例httpClient调用 http/https实例 忽略SSL验证

    要创建一个简单的HttpClient实例,你需要以下步骤: 1. 引入Apache HttpClient库: 在你的项目中,确保已经添加了Apache HttpClient的依赖。例如,如果你使用的是Maven,可以在pom.xml文件中添加以下依赖: ```...

    HttpClient实例+必备3个jar包

    在"HttpClient实例+必备3个jar包"的项目中,包含了以下关键知识点: 1. **HttpClient类库**:HttpClient库提供了丰富的API,可以创建复杂的HTTP请求,包括设置请求头、携带请求体、处理重定向、管理Cookie等。通过...

    C#HTTPclient 实例应用

    // 从依赖注入容器中获取HttpClient实例 var httpClient = serviceProvider.GetService&lt;HttpClient&gt;(); ``` ### 2. 发送GET请求 `HttpClient`提供了`GetAsync`方法用于发送GET请求。下面是如何使用它的示例: ```...

    JavaHttpClient实例

    本实例将深入探讨如何在Java中使用HttpClient进行网络通信。 首先,你需要在项目中引入HttpClient的相关依赖。如果是Maven项目,可以在pom.xml文件中添加以下依赖: ```xml &lt;groupId&gt;org.apache.httpcomponents ...

    httpclient实例

    实现调用远程servlet方法的实例。有详细注释。和依赖jar包。 //servlet路径 String url = "http://localhost:8080/zfw/servlet/CustomQueryServlet"; //参数一为表名,参数二为字段名 String params[] = {"ZC_...

    HttpClient用法,实例

    HttpClient用法,实例 HttpClient用法,实例 HttpClient用法,实例 HttpClient用法,实例

    httpClient 网络传输的实例

    1. **创建HttpClient对象**:首先,我们需要创建一个HttpClient实例,这通常是通过HttpClientBuilder或HttpAsyncClientBuilder构建的。例如: ```java CloseableHttpClient httpClient = HttpClients.create...

    Android的HttpClient开发实例

    本开发实例将带你深入理解如何在Android项目中使用`HttpClient`进行网络请求,实现数据的获取和上传。 首先,`HttpClient`是Apache的一个开源项目,它提供了一个强大的API来处理HTTP协议。`commons-httpclient-3.1....

    HttpClient模拟登录实例

    创建HttpClient实例是模拟登录的第一步。这可以通过`HttpClientBuilder`或直接使用`HttpClient`的静态工厂方法完成。例如: ```java CloseableHttpClient httpClient = HttpClients.createDefault(); ``` 2. **...

    httpClient4.3.6包和实例

    1. **HttpClient实例**:HttpClient是线程不安全的,所以通常推荐每个请求创建一个新实例。`HttpClientBuilder`类可以用来构建自定义配置的客户端实例。 2. **请求执行器(RequestExecutor)**:处理HTTP请求和响应...

    HttpClient应用实例2

    读者将学习到如何配置HttpClient实例,设置请求参数,处理响应,并通过源码分析了解其实现细节。同时,还会接触到日志管理和编码转换的相关知识,这些都是Java开发中与网络通信密切相关的技能。

    HttpClient之Https应用实例

    HttpClient之Https应用实例~ 包含: HttpClient 使用代理访问Https HttpClient 信任所有主机-对于任何证书都不做检查 HttpClient 允许所有证书的校验(包括过期证书)

    jsp 中HttpClient中的POST方法实例详解.docx

    1. **创建HttpClient实例和PostMethod实例** ```java String url = ".newsmth.net/bbslogin2.php"; HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod(url); ``` 2. **设置...

    java 中HttpClient传输xml字符串实例详解

    在本实例中,我们将讲解如何使用HttpClient来传输XML字符串。首先,我们需要确保引入了正确的依赖,包括Apache HttpClient、HttpMime、Commons Lang以及其他的辅助库。以下是POM文件中的依赖配置: ```xml &lt;!-- ....

    【ASP.NET编程知识】.NET CORE HttpClient的使用方法.docx

    命名客户端是指我们可以给 HttpClient 实例起一个名称,以便在后续的使用中可以根据名称来获取对应的 HttpClient 实例。我们可以使用以下代码来添加命名客户端: `services.AddHttpClient(Constants.SERVICE_USER...

    httpclient httpclient.jar

    通常,我们创建一个HttpClient实例,并设置其配置参数,如连接超时、重试策略等。 2. **HttpHost**:表示目标HTTP服务器的信息,包括主机名、端口和协议(HTTP或HTTPS)。 3. **HttpGet/HttpPost**:这些是执行...

Global site tag (gtag.js) - Google Analytics