美文网首页
java的http通信

java的http通信

作者: neesonD | 来源:发表于2017-02-12 21:27 被阅读0次

使用URLConnection与httpClient发送get/post请求


使用URLConnection实现GET/POST请求

  1. 实例化一个java.net.URL对象;
  2. 通过URL对象的openConnection()方法得到一个java.net.URLConnection;
  3. 通过URLConnection对象的getInputStream()方法获得输入流;
  4. 读取输入流;
  5. 关闭资源
//GET
public void get() throws Exception{

    URL url = new URL("http://127.0.0.1/http/test?name=neeson&age=10");
    URLConnection urlConnection = url.openConnection();                                                    // 打开连接
    BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"utf-8")); // 获取输入流
    String line = null;
    StringBuilder sb = new StringBuilder();
    while ((line = br.readLine()) != null) {
        sb.append(line + "\n");
    }

    System.out.println(sb.toString());
}
//POST
public void post() throws IOException{

    URL url = new URL("http://127.0.0.1/http/test");
    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

    httpURLConnection.setDoInput(true);
    httpURLConnection.setDoOutput(true);        // 设置该连接是可以输出的
    httpURLConnection.setRequestMethod("POST"); // 设置请求方式
    httpURLConnection.setRequestProperty("charset", "utf-8");

    PrintWriter pw = new PrintWriter(new BufferedOutputStream(httpURLConnection.getOutputStream()));
    pw.write("name=neeson");                   // 向连接中输出数据(相当于发送数据给服务器)
    pw.write("&age=14");
    pw.flush();
    pw.close();

    BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"utf-8"));
    String line = null;
    StringBuilder sb = new StringBuilder();
    while ((line = br.readLine()) != null) {    // 读取数据
        sb.append(line + "\n");
    }

    System.out.println(sb.toString());
}

使用httpclient进行GET/POST请求

//GET
public void httpclientGet() throws Exception{

    // 创建HttpClient对象
    HttpClient client = HttpClients.createDefault();

    // 创建GET请求(在构造器中传入URL字符串即可)
    HttpGet get = new HttpGet("http://127.0.0.1/http/test?name=admin&age=40");

    // 调用HttpClient对象的execute方法获得响应
    HttpResponse response = client.execute(get);

    // 调用HttpResponse对象的getEntity方法得到响应实体
    HttpEntity httpEntity = response.getEntity();

    // 使用EntityUtils工具类得到响应的字符串表示
    String result = EntityUtils.toString(httpEntity,"utf-8");
    System.out.println(result);
}
//POST
public void httpclientPost() throws Exception{

    // 创建HttpClient对象
    HttpClient client = HttpClients.createDefault();

    // 创建POST请求
    HttpPost post = new HttpPost("http://127.0.0.1/http/test");

    // 创建一个List容器,用于存放基本键值对(基本键值对即:参数名-参数值)
    List<BasicNameValuePair> parameters = new ArrayList<>();
    parameters.add(new BasicNameValuePair("name", "张三"));
    parameters.add(new BasicNameValuePair("age", "25"));

    // 向POST请求中添加消息实体
    post.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));

    // 得到响应并转化成字符串
    HttpResponse response = client.execute(post);
    HttpEntity httpEntity = response.getEntity();
    String result = EntityUtils.toString(httpEntity,"utf-8");
    System.out.println(result);
}

相关文章

  • java的http通信

    使用URLConnection与httpClient发送get/post请求 使用URLConnection实现G...

  • Java使用HTTP的get方法读取网络数据、使用post方法与

    1.Java使用HTTP的get方法读取网络数据 2.Java使用HTTP的post方法与网络交互通信

  • 并发编程

    (1)JAVA内存模型(JMM) #java当中的线程通讯和消息传递(通信) http://www.cnblogs...

  • HTTP通信

    如何使用HTTPURLConnetion 访问接口:https://blog.csdn.net/huaduoton...

  • HTTP通信

    1. HTTP状态码 状态码是客户端向服务器请求时,描述返回的请求结果。借助状态码,用户可以知道服务器是正常的处理...

  • HTTP通信

    HTTP通信过程包括从客户端发往服务器端的请求以及从服务器端返回客户端的响应. 1. HTTP报文结构 用于HTT...

  • Http通信

    Unity使用Http很常见。 原生提供的WWW功能不多,也不支持超时检测。后来有UnityWebRequest,...

  • Android的网络编程

    Android的网络编程 HTTP通信方式、Socket通信方式 HTTP的简介: 首先HTTP全称是Hyper ...

  • 02-25星期一 随堂笔记

    Java Web掌握什么? 1.网络通信协议:http2.服务器:Tomcat jetty weblogic ng...

  • Java中使用OkHttp进行Http通信

    提供一个OkHttp的工具类方法。 1. 引入依赖 2. 工具类代码 工具类需要创建一个单例的OkHttp的客户端...

网友评论

      本文标题:java的http通信

      本文链接:https://www.haomeiwen.com/subject/ryzaittx.html