java Http请求

maven

     <dependency>
		  <groupId>org.apache.httpcomponents</groupId>
		  <artifactId>httpclient</artifactId>
		  <version>4.5.1</version>
		  <scope>compile</scope>
	  </dependency>

请求头  content-type 类型 https://www.runoob.com/http/http-content-type.html

multipart/form-data : 需要在表单中进行文件上传时,就需要使用该格式

请求头 Accept代表发送端(客户端)希望接受的数据类型。

 

使用

public static String post(String url, String jsonstr, Map<String, String> heads) throws IOException {
        String result = "";
        CloseableHttpResponse httpResponse = null;
        CloseableHttpClient httpClient = null;
        HttpPost httpPost = null;
        try {

            // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
            httpClient = HttpClientBuilder.create().build();
            String charset = "utf-8";
            

            // 定义请求的参数
            //URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build();
            //httpGet = new HttpGet(uri);

            // 创建Post请求
            httpPost = new HttpPost(url);
            //解析对象
            StringEntity entity = new StringEntity(jsonstr, charset);
            httpPost.setEntity(entity);
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-type", "application/json");
            if (heads != null) {
                for (String headKey : heads.keySet()) {
                    httpPost.setHeader(headKey, heads.get(headKey));
                }
            }
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(1000) //连接超时时间
                    .setConnectionRequestTimeout(1000) //从连接池中取的连接的最长时间
                    .setSocketTimeout(10 *1000) //数据传输的超时时间
                    .build();

            httpResponse = httpClient.execute(httpPost);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                result = EntityUtils.toString(httpResponse.getEntity(), charset);
            }
            result = httpResponse.toString();
        }finally {
            if(httpResponse != null){
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                if(httpPost != null){
                    httpPost.clone();
                }
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
            if(httpClient != null){
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;

配置API  RequestConfig()

protected  RequestConfigclone()
           
static RequestConfig.Buildercopy(RequestConfig config)
           
static RequestConfig.Buildercustom()
           
 intgetConnectionRequestTimeout()
          返回从连接管理器请求连接时使用的超时(以毫秒为单位)。
 intgetConnectTimeout()
          确定连接建立之前的超时时间(以毫秒为单位)。
 StringgetCookieSpec()
          确定用于HTTP状态管理的cookie规范的名称。
 InetAddressgetLocalAddress()
          返回用于执行请求的本地地址。
 intgetMaxRedirects()
          返回要遵循的最大重定向数。
 org.apache.http.HttpHostgetProxy()
          返回用于请求执行的HTTP代理。
 Collection<String>getProxyPreferredAuthSchemes()
          确定使用代理主机进行身份验证时支持的身份验证方案的优先顺序。
 intgetSocketTimeout()
          定义套接字超时(SO_TIMEOUT),以毫秒为单位,这是等待数据的超时,或者换句话说,是两个连续数据包之间的最大时间段不活动。
 Collection<String>getTargetPreferredAuthSchemes()
          确定使用目标主机进行身份验证时支持的身份验证方案的优先顺序。
 booleanisAuthenticationEnabled()
          确定是否应自动处理身份验证。
 booleanisCircularRedirectsAllowed()
          确定是否应允许循环重定向(重定向到同一位置)。
 booleanisContentCompressionEnabled()
          确定是否请求目标服务器压缩内容。
 booleanisDecompressionEnabled()
          不推荐使用。 (4.5)使用isContentCompressionEnabled()
 booleanisExpectContinueEnabled()
          确定是否为实体封装方法启用了“期望:100-连续”握手。
 booleanisNormalizeUri()
          确定客户端是否应规范请求中的URI。
 booleanisRedirectsEnabled()
          确定是否应自动处理重定向。
 booleanisRelativeRedirectsAllowed()
          确定是否应拒绝相对重定向。
 booleanisStaleConnectionCheckEnabled()
          不推荐使用。 (4.4)使用PoolingHttpClientConnectionManager.getValidateAfterInactivity()
 StringtoString()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值