第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在 java 中使用 HttpURLConnection 發(fā)送帶有 GET 請(qǐng)求的請(qǐng)求體

在 java 中使用 HttpURLConnection 發(fā)送帶有 GET 請(qǐng)求的請(qǐng)求體

MMTTMM 2023-04-13 10:45:29
請(qǐng)不要將我的問(wèn)題與使用 HttpURLConnection 發(fā)送帶有 POST 請(qǐng)求的正文混淆。我想使用 HttpURLConnection 發(fā)送帶有 GET 請(qǐng)求的正文。這是我正在使用的代碼。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;}實(shí)際情況是根據(jù) connection.getInputStream()和connection.getOutPutStream() 自動(dòng)識(shí)別請(qǐng)求類型。當(dāng)您調(diào)用connection.getOutPutStream()時(shí),請(qǐng)求類型會(huì)自動(dòng)設(shè)置為POST,即使您已使用connection.setRequestMethod("GET")將請(qǐng)求類型明確設(shè)置為GET。問(wèn)題是我正在使用第 3 方 Web 服務(wù) (API),它接受請(qǐng)求參數(shù)作為 GET 請(qǐng)求的正文<get-request>/myAPIEndPointbody = parameter1=value as application/x-www-form-urlencoded<response>{json}我很清楚大多數(shù)情況下 GET 沒有請(qǐng)求主體,但許多 Web 服務(wù)經(jīng)常使用帶有參數(shù)的 GET 請(qǐng)求作為主體而不是查詢字符串。請(qǐng)指導(dǎo)我如何在不使用任何第 3 方庫(kù)(OkHttp、Retrofit、Glide 等)的情況下在 android 中發(fā)送帶正文的 GET 請(qǐng)求
查看完整描述

1 回答

?
精慕HU

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊

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


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 請(qǐng)求轉(zhuǎn)儲(chǔ)


GET <Resource Address> HTTP/1.1

User-Agent: Java Socket

Content-Type: application/x-www-form-urlencoded


parameter1=value&parameter2=value2


查看完整回答
反對(duì) 回復(fù) 2023-04-13
  • 1 回答
  • 0 關(guān)注
  • 399 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)