亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 java 中使用 HttpURLConnection 發送帶有 GET 請求的請求體

在 java 中使用 HttpURLConnection 發送帶有 GET 請求的請求體

MMTTMM 2023-04-13 10:45:29
請不要將我的問題與使用 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,即使您已使用connection.setRequestMethod("GET")將請求類型明確設置為GET。問題是我正在使用第 3 方 Web 服務 (API),它接受請求參數作為 GET 請求的正文<get-request>/myAPIEndPointbody = parameter1=value as application/x-www-form-urlencoded<response>{json}我很清楚大多數情況下 GET 沒有請求主體,但許多 Web 服務經常使用帶有參數的 GET 請求作為主體而不是查詢字符串。請指導我如何在不使用任何第 3 方庫(OkHttp、Retrofit、Glide 等)的情況下在 android 中發送帶正文的 GET 請求
查看完整描述

1 回答

?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

使用這段代碼你需要做一些修改,但它會完成工作。


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


查看完整回答
反對 回復 2023-04-13
  • 1 回答
  • 0 關注
  • 401 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號