java 发送 http 请求

POM 依赖

<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.6</version>
        </dependency>

不带参数 get 请求

public void doGet() throws Exception {

        // 创建一个httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建一个GET对象
        HttpGet get = new HttpGet("http://www.sogou.com");
        // 执行请求
        CloseableHttpResponse response = httpClient.execute(get);
        // 取响应的结果
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println(statusCode);
        HttpEntity entity = response.getEntity();
        String string = EntityUtils.toString(entity, "utf-8");
        System.out.println(string);
        // 关闭httpclient
        response.close();
        httpClient.close();

    }

带参数的 GET 请求

public void doGetWithParam() throws Exception {

        // 创建一个httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建一个uri对象
        URIBuilder uriBuilder = new URIBuilder("http://www.sogou.com/web");
        uriBuilder.addParameter("query", "花千骨");
        HttpGet get = new HttpGet(uriBuilder.build());
        // 执行请求
        CloseableHttpResponse response = httpClient.execute(get);
        // 取响应的结果
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println(statusCode);
        HttpEntity entity = response.getEntity();
        String string = EntityUtils.toString(entity, "utf-8");
        System.out.println(string);
        // 关闭httpclient
        response.close();
        httpClient.close();
    }

不带参数的 POST 请求

public void doPost() throws Exception {

        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建一个post对象
        HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.html");
        // 执行post请求
        CloseableHttpResponse response = httpClient.execute(post);
        String string = EntityUtils.toString(response.getEntity());
        System.out.println(string);
        response.close();
        httpClient.close();
    }

带参数的 POST 请求

public void doPostWithParam() throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建一个post对象
        HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.html");
        // 创建一个Entity。模拟一个表单
        List<NameValuePair> kvList = new ArrayList<NameValuePair>();
        kvList.add(new BasicNameValuePair("username", "zhangsan"));
        kvList.add(new BasicNameValuePair("password", "123"));
        // 包装成一个Entity对象
        StringEntity entity = new UrlEncodedFormEntity(kvList, "utf-8");
        // 设置请求的内容
        post.setEntity(entity);
        // 执行post请求
        CloseableHttpResponse response = httpClient.execute(post);
        String string = EntityUtils.toString(response.getEntity());
        System.out.println(string);
        response.close();
        httpClient.close();
    }

原文地址:https://www.cnblogs.com/huanggy/p/9636461.html

时间: 2024-11-13 06:44:23

java 发送 http 请求的相关文章

关于JAVA发送Https请求(HttpsURLConnection和HttpURLConnection)

关于JAVA发送Https请求(HttpsURLConnection和HttpURLConnection) [转] https协议对于开发者而言其实只是多了一步证书验证的过程.这个证书正常情况下被jdk/jre/security/cacerts所管理.里面证书包含两种情况: 1.机构所颁发的被认证的证书,这种证书的网站在浏览器访问时https头显示为绿色如百度 2.个人所设定的证书,这种证书的网站在浏览器里https头显示为红色×,且需要点击信任该网站才能继续访问.而点击信任这一步的操作就是我们

Java发送HTTPS请求

前言 上篇文章介绍了 java 发送 http 请求,大家都知道发送http是不安全的 .我也是由于对接了其他企业后总结了一套发送 https的工具.大家网上找方法很多的,但是可不是你粘过来就能用啊,我也是踩过坑的,所以我这个工具,只要粘贴到你们自己项目里就可以用.我的工具跟网上没什么区别,唯一的区别是我亲身实战过,把需要注意的细节列出来,不让大家浪费时间.   正文 本文只介绍 发送 post 请求,既然选择了 https 就不会用get,因为get也是不安全的.   读前须知 我会把需要依赖

Java发送Http请求

package com.liuyu.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; public c

java 发送http请求

package cn.go4mi; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; public class H

java 发送 http请求——HttpClient

使用HttpClient来发送Http请求 引入两个包:[1]org.apache.httpcomponents.httpclient_x.x.x.jar  [2]org.apache.httpcomponents.httpcore_x.x.x.jar 下载链接:Apache HttpComponents - HttpComponents Downloads 参考文档:[1]HttpClient Tutorial  [2]HttpClient Example 1 package http; 2

JAVA发送HttpClient请求及接收请求结果过程

下面为本人自己在工作中写业务代码的,并不是通用的,只供自己下次使用时能有个查找地,及正处在困扰中的程序员借鉴. 推荐好点博客给大家看看: http://blog.sina.com.cn/s/blog_75a8cfac01013aim.html http://blog.csdn.net/fireelement/article/details/2497136 http://www.2cto.com/kf/201206/136879.html http://284772894.iteye.com/bl

Java发送POST请求,参数为JSON格式,并接收返回JSON数据

原文地址:https://blog.csdn.net/qq_26975307/article/details/82713725 /** * 发送post请求 * @param url 路径 * @param jsonObject 参数(json类型) * @param encoding 编码格式 * @return * @throws ParseException * @throws IOException */ public static String send(String url, JSO

java发送 get请求

package com.java.base; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class javaHTTPHandle { /**     * 向指定URL发送GET方法的请求     * @param url     *            发送请求的URL     *

java发送http请求(提交form表单)

http://hbiao68.iteye.com/blog/1973914 写一个servlet用于测试请求 Java代码   import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import