【问题标题】:Send request body with GET request using HttpURLConnection in java在 java 中使用 HttpURLConnection 发送带有 GET 请求的请求正文
【发布时间】:2019-07-21 21:26:14
【问题描述】:

请不要将我的问题与使用 HttpURLConnection 发送带有 POST 请求的正文混淆。

我想使用 HttpURLConnection 发送带有 GET 请求的正文。这是我正在使用的代码。

public static String makeGETRequest(String endpoint, String encodedBody) {
    String responseJSON = null;
    URL url;
    HttpURLConnection connection;

    try {
        url = new URL(endpoint);
        connection = (HttpURLConnection) url.openConnection();

        connection.setInstanceFollowRedirects(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        connection.connect();
        OutputStream outputStream = connection.getOutputStream();
        outputStream.write(encodedBody.getBytes());
        outputStream.flush();

        Util.log(connection,connection.getResponseCode()+":"+connection.getRequestMethod());

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String responseChunk = null;
        responseJSON = "";
        while ((responseChunk = bufferedReader.readLine()) != null) {
            responseJSON += responseChunk;
        }

        bufferedReader.close();
        connection.disconnect();

    } catch (Exception e) {
        e.printStackTrace();
        Util.log(e, e.getMessage());
    }

    return responseJSON;
}

会根据 connection.getInputStream()connection.getOutPutStream() 自动识别请求类型。

当您调用 connection.getOutPutStream() 时,请求类型会自动设置为 POST,即使您已将请求类型明确设置为 GET使用 connection.setRequestMethod("GET")

问题是我正在使用 3rd 方 Web 服务 (API),它接受请求参数作为带有 GET 请求的正文

<get-request>
/myAPIEndPoint
body = parameter1=value as application/x-www-form-urlencoded

<response>
{json}

我很清楚大多数情况下 GET 没有请求正文,但许多 Web 服务经常使用带有参数的 GET 请求作为正文而不是查询字符串。请指导我如何在不使用任何第三方库(OkHttp、Retrofit、Glide 等)的情况下在 android 中发送带有 body 的 GET 请求

【问题讨论】:

标签: java android api web-services get


【解决方案1】:

使用此代码,您需要进行一些修改,但它会完成工作。

package com.kundan.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class GetWithBody {

    public static final String TYPE = "GET ";
    public static final String HTTP_VERSION = " HTTP/1.1";
    public static final String LINE_END = "\r\n";

    public static void main(String[] args) throws Exception {
        Socket socket = new Socket("localhost", 8080); // hostname and port default is 80
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write((TYPE + "<Resource Address>" + HTTP_VERSION + LINE_END).getBytes());// 
        outputStream.write(("User-Agent: Java Socket" + LINE_END).getBytes());
        outputStream.write(("Content-Type: application/x-www-form-urlencoded" + LINE_END).getBytes());
        outputStream.write(LINE_END.getBytes()); //end of headers
        outputStream.write(("parameter1=value&parameter2=value2" + LINE_END).getBytes()); //body 
        outputStream.flush();

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        StringBuilder builder = new StringBuilder();
        String read = null;
        while ((read = bufferedReader.readLine()) != null) {
            builder.append(read);
        }

        String result = builder.toString();
        System.out.println(result);
    }
}

这是原始 HTTP 请求转储

GET <Resource Address> HTTP/1.1
User-Agent: Java Socket
Content-Type: application/x-www-form-urlencoded

parameter1=value&parameter2=value2

注意:如果你想要https连接,这是http请求请参考链接SSLSocketClient

【讨论】:

    猜你喜欢
    • 2018-01-25
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 2020-04-25
    • 2016-08-24
    • 1970-01-01
    相关资源
    最近更新 更多