使用httpclient发送http请求

先来个httpclient的maven依赖

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.3</version>
</dependency>

SimpleHttpClient.java

package com.openapi.TestPojo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class SimpleHttpClient {

    public String post(String url, Map<String,String> params) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        Set<String> keySet = params.keySet();
        for(String key : keySet) {
            nvps.add(new BasicNameValuePair(key, params.get(key)));
        }
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
        String body = invoke(httpclient, httpPost);
        try {
            httpclient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return body;
    }

    public String get(String url) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        String body = invoke(httpclient, httpGet);
        try {
            httpclient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return body;
    }

    public String invoke(CloseableHttpClient httpclient, HttpUriRequest request) {
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(request);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpEntity entity = response.getEntity();
        String body = null;
        try {
            body = EntityUtils.toString(entity);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return body;
    }

}

参考链接:

官方文档 http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/

http://witcheryne.iteye.com/blog/1135817

使用httpclient发送http请求

时间: 2024-10-09 07:30:51

使用httpclient发送http请求的相关文章

Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)

[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4006009.html 联系方式:[email protected] [系列]Android系列之网络:(持续更新) Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据) Android系列之网络(二)----HTTP请求头与响应头 Android

android使用apache httpclient发送post请求

package com.liuc; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.ht

使用HttpClient发送GET请求

HttpRequestMessage http_req_msg = new HttpRequestMessage(); http_req_msg.Method = HttpMethod.Get; http_req_msg.Headers.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); http_req_msg.RequestUr

Httpclient发送json请求

一.Httpclient发送json请求 public String RequestJsonPost(String url){    String strresponse = null;    try{        HttpClient hc = new DefaultHttpClient();       HttpPost hp = new HttpPost(url);       JSONObject jsonParam = new JSONObject();       jsonPara

HTTPClient 发送HTTPS请求

HTTPClient 发送HTTP请求就不多说了, 现在给出发送HTTPS请求, 主要思路是忽略证书验证. /** * * @param url * @param contextType "image/jpeg","application/Json" * @return */ public static byte[] sendHttpsGetUrl(HttpClient httpClient1 ,String url,String contextType) { //

.net core使用HttpClient发送代理请求_程序内抓包_Fiddler抓包

原文:.net core使用HttpClient发送代理请求_程序内抓包_Fiddler抓包 前言:  通过Fiddler抓取浏览器请求数据,相信大家已经都会用了,我们知道Fiddler是通过在本机计算器添加一个默认的代理服务器来实现的抓包数据的,端口号为:8888. 其实当我们打开Fiddler的设置也可以看到: 然后查看本地计算器的网络代理设置: 基于上面的原理,Fiddler就实现了经过本机计算器请求的数据抓包了... 那么,我们通过C#代码,在.net Core中使用HttpClient

通过 Apache Commons HttpClient 发送 HTTPS 请求

1.通过 HTTPS 发送 POST 请求: 2.HTTPS 安全协议采用 TLSv1.2: 3. 使用代理(Proxy)进行 HTTPS 访问: 4.指定 Content-Type 为:application/x-www-form-urlencoded: 5.HTTPS  请求时加载客户端证书(Client Certificate): 6.忽略服务器端证书链(Server Certificate Chain)的校验(Validate). public static void main(Stri

java httpclient发送json 请求 ,go服务端接收

/***java客户端发送http请求*/package com.xx.httptest; /** * Created by yq on 16/6/27. */ import java.io.IOException; import java.net.URLEncoder; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.GetMethod; import org.apache

httpclient发送request请求时设置header和timeout

package com.xxx.xxx.common; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Map; import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.Htt