java发送get和post请求

不问归期 提交于 2019-12-06 15:03:11

java发送get和post请求

博文

JAVA的GET和POST请求实现方式
https://blog.csdn.net/u012513972/article/details/79569888
package com.mozq.http.http_01.client;

import com.mozq.http.http_01.util.HttpTool;

import java.io.IOException;
import java.net.URLEncoder;

public class Client_01 {
    public static void main(String[] args) throws IOException {
        String url = "http://localhost:7001/demo?"
                + "name=" + URLEncoder.encode("刘备", "UTF-8");
        String res = HttpTool.get(url, null);
        System.out.println(res);
    }
}

package com.mozq.http.http_01.util;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Objects;

/**
 * @description:
 */
public class HttpTool {
    public static String get(String urlStr, String param) throws IOException {
        String reqUrl = Objects.nonNull(param) && param.trim().length() > 0
                ? urlStr + "?" + param
                : urlStr;
        InputStream is = null;
        try {
            StringBuilder result = new StringBuilder();
            URL url = new URL(reqUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            /*
            private static final String[] methods = {
                    "GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE"
            };
            */
            conn.setRequestMethod("GET");
            conn.setDoOutput(true);
            conn.setDoInput(true);

            conn.connect();
            is = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line = null;
            while ((line = reader.readLine()) != null){
                result.append(line);
                result.append(System.lineSeparator());
            }
            return result.toString();
        } finally{
            if(Objects.nonNull(is)){
                is.close();
            }
        }
    }
}
public class Client_01 {
    public static void main(String[] args) throws IOException {
        String url = "http://localhost:7001/demo";
        String res = HttpTool.get(url, "name=刘备");
        System.out.println(res);
    }
    /*
    Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: http://localhost:7001/demo?name=刘备
    原因:参数中有中文。
    方案: java.net.URLEncoder.encode("刘备", "UTF-8");
    */
}

public class HttpTool {
    public static String get(String urlStr, String param) throws IOException {
        URL url = new URL(urlStr + "?" + param);
        URLConnection conn = url.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder result = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null){
            result.append(line);
        }
        return result.toString();
    }
}

bugs

Caused by: java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)
Caused by: java.net.ProtocolException: Cannot write output after reading input.
错误代码:
System.out.println(JSONObject.toJSONString(conn));

url中含有非法字符

java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!