JAVA使用Post方法进行API调用

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Tochatluck {
    public static void send(String url) throws Exception {	
        //地址
        String straddr = "";
        StringBuffer sb = new StringBuffer(straddr);
        System.out.println("URL:"+sb);

        //发送请求
        URL url = new URL(sb.toString());
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        BufferedReader in = new BufferedReader(new InputStreamReader(
        url.openStream()));
        //返回结果
        String inputline = in.readLine();
        System.out.println("Response:"+inputline);
    }

    public static void main(String[] args) {
    try {
        String url= "";
        send(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");设置请求头信息

发布了42 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_39361915/article/details/97947129