Java:HttpURLConnection发送GET和POST请求

简介: Java:HttpURLConnection发送GET和POST请求

发送GET请求

package demo;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;


public class HttpDemo {
    public static void main(String[] args) throws IOException {
        String url = "https://www.baidu.com/";

        // 得到connection对象
        URL httpUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
        
        //连接
        connection.connect();
        
        // 获取状态码 响应结果
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

            String line = null;

            StringBuffer buffer = new StringBuffer();

            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

            reader.close();

            System.out.println(buffer.toString());

        }

        // 断开连接
        connection.disconnect();

    }
}

发送POST请求

package demo;


import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;


public class HttpDemo {
public static void main(String[] args) throws IOException {
String url = "http://httpbin.org/post";;

//得到connection对象
URL httpUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();

//设置请求方式
connection.setRequestMethod("POST");
connection.setDoOutput(true);

// 设置请求头
connection.setRequestProperty("Accept", "/");

// 设置请求体
String body = "name=Tom&age=23";
OutputStream outputStream = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.write(body);
writer.close();

//连接
connection.connect();

// 获取状态码 响应结果
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

String line = null;

StringBuffer buffer = new StringBuffer();

while ((line = reader.readLine()) != null) {
buffer.append(line);
}

reader.close();

System.out.println(buffer.toString());

}

// 断开连接
connection.disconnect();

}
}


            </div>
目录
相关文章
|
6月前
|
JSON Java 数据格式
Java如何发起http的get请求的实现
Java如何发起http的get请求的实现
|
12月前
|
JSON 网络协议 Java
OkHttp3发送http请求在Java中的使用方法
记录总结一下Http的get请求和post请求的使用方法和问题解决
473 0
|
9月前
|
Java
RestTemplate实践:Java中如何发送Post请求?
RestTemplate实践:Java中如何发送Post请求?
126 0
|
Java Apache Spring
Java发送Http请求(HttpClient)
Java发送Http请求(HttpClient)
7448 1
|
9月前
|
Java API Apache
Java HTTP请求封装的方法及实现
在Java开发中,我们经常需要与服务器进行数据交互,发送HTTP请求是其中常见的一种方式。为了简化开发过程,我们可以封装HTTP请求的方法,让调用者只需要关注业务逻辑而不用关心底层的细节实现。本文将介绍一种基于Java的HTTP请求封装方法及其实现。
195 2
|
JSON Java 数据格式
JAVA获取GET和POST请求参数
JAVA获取GET和POST请求参数
1852 0
|
11月前
|
JSON 网络协议 Java
java发送Http请求
java发送Http请求
|
JSON Java 程序员
在Java中,使用HttpUtils实现发送HTTP请求
HTTP请求,在日常开发中,还是比较常见的,今天给大家分享HttpUtils如何使用。
540 0
在Java中,使用HttpUtils实现发送HTTP请求
|
Java
Java:HttpURLConnection发送GET和POST请求
Java:HttpURLConnection发送GET和POST请求
220 0
|
Java API Python
Java:retrofit2发送http网络请求
Java:retrofit2发送http网络请求
210 0