`

Java HttpUtil

阅读更多
1 Maven引用
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
</dependency>


2 参考地址
http://hc.apache.org/


3 HttpUtil

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @author Administrator
 * @version 1.0.0
 * @description Http传输工具
 * @datetime 2017/4/15 18:09
 */
public class HttpUtil {

    /**
     * This example
     * the process of processing the HTTP response and releasing associated resources.
     */
    public static void httpGet() throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet httpget = new HttpGet("http://httpbin.org/");

            System.out.println("Executing request " + httpget.getRequestLine());

            // Create a custom response handler
            ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

                @Override
                public String handleResponse(
                        final HttpResponse response) throws ClientProtocolException, IOException {
                    int status = response.getStatusLine().getStatusCode();
                    if (status >= 200 && status < 300) {
                        HttpEntity entity = response.getEntity();
                        return entity != null ? EntityUtils.toString(entity) : null;
                    } else {
                        throw new ClientProtocolException("Unexpected response status: " + status);
                    }
                }

            };
            String responseBody = httpclient.execute(httpget, responseHandler);
            System.out.println("----------------------------------------");
            System.out.println(responseBody);
        } finally {
            httpclient.close();
        }
    }


    /**
     * post json格式的字符串
     * @throws IOException
     */
    public static void httpPost() throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();

        try {
            HttpPost httppost = new HttpPost("http://httpbin.org/");

            //Interface HttpEntity
            //AbstractHttpEntity, BasicHttpEntity, BufferedHttpEntity, ByteArrayEntity, EntityTemplate, FileEntity, HttpEntityWrapper, InputStreamEntity, SerializableEntity, StringEntity
            httppost.setEntity(new StringEntity("{a:123}", ContentType.APPLICATION_JSON));
            System.out.println("Executing request " + httppost.getRequestLine());

            //All Known ResponseHandler Implementing Classes:
            //AbstractResponseHandler, BasicResponseHandler
            String responseBody = httpclient.execute(httppost, new BasicResponseHandler());
            System.out.println("----------------------------------------");
            System.out.println(responseBody);
        } finally {
            httpclient.close();
        }
    }


}

0
0
分享到:
评论

相关推荐

    HttpUtil.java

    非常齐全的http工具类,可以发送/接收各种请求。包括文件的发送,获取,带token验证的请求等

    HTTPUtil.java

    用于HttplClient 请求,执行post 和 get 请求的Util 工具类

    httpUtil httpclient 登陆携带cookie访问下一个连接

    Java,通过 httpclient 获取 cookie 模拟登录 ,登录后携带cookie发起下一个请求

    JavaHttpUtil.java

    Java模拟发送get请求 Java模拟发送post请求 HTTP工具 返回值统一为String,所以请求接口要注意为 返回为json或者xml的接口。

    http依赖jar包.zip

    import java.io.IOException; import java.io.Serializable; import java.util.HashMap; import java.util.Map; import com.google.gson.JsonObject; import com.google.gson.JsonArray; import ...

    HttpClientUtil工具类,调用第三方接口

    该工具类是java 调用第三方接口时需要使用到的。HttpClientUtil 包含get和post方法。

    Java Http工具类HttpClientUtil

    多年积累,功能比较强大,可设置路由连接数,时间,请求类型包括get,post, 参数包括urlcode,map,json,xml。注释很清楚。

    leona-httputil:简单的Java Http Util

    leona-httputil 简单的Java Http Util更少的代码,更舒适(LCMC) 此leona-httputil规范的主要目的是通过单行调用使开发人员的工作变得更加轻松。 尤其是Http util库非常繁琐,包含许多行代码和示例。 Leon ...

    java-http-requests:Java HTTP请求util

    它基本上是一个jar文件,可以导入到Java项目中,该文件提供了能够发送[GET,POST,PUT,DELETE]请求的功能。 用法 要使用jar文件,仅需要将其作为引用库导入到项目中。 它提供的功能是: sendRequest(String url...

    java-httputil.rar

    java模拟浏览器请求,包括post、get等方式..

    java util工具类

    java util帮助类,包括日期工具类、字符串处理工具类、上传工具类、http请求工具类...HttpUtil.java MD5Util.java Pagination.java PropertiesUtil.java RegUtil.java StringUtil.java UploadUtil.java UUIDUtils.java

    java http token请求代码实例

    主要介绍了java http token请求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    HttpUtil(1).java

    HttpUtil(1).java

Global site tag (gtag.js) - Google Analytics