gpt4 book ai didi

java.io.IOException : Server returned HTTP response code: 403 for URL

转载 作者:太空宇宙 更新时间:2023-11-04 13:34:31 35 4
gpt4 key购买 nike

我正在尝试使用java代码发送HTTP POST请求。但我越来越

 java.io.IOException: Server returned HTTP response code: 403 for URL

代码

    try {

String webPage = "https://testweb/test.apk?overwrite=true --data-binary @app0720.apk";
String name = "testname";
String password = "cpaad4%675c";

String authString = name + ":" + password;
System.out.println("Auth string: " + authString);

Base64 b = new Base64();
String authStringEnc = b.encodeAsString(new String(authString).getBytes());

System.out.println("Base64 encoded auth string: " + authStringEnc);

URL url = new URL(webPage);
URLConnection urlConnection = url.openConnection();
urlConnection.addRequestProperty("Authorization", "Basic " + authStringEnc);
urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
urlConnection.addRequestProperty("Accept", "application/octet-stream");
urlConnection.connect();

InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int numCharsRead;
char[] charArray = new char[1024];
StringBuffer sb = new StringBuffer();
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
String result = sb.toString();

System.out.println("---------------------------------------------");
System.out.println("Response from the server: " + result);

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

但是当我在休息控制台中尝试时,它变得正常了。

来自 REST 控制台的请求 header

Accept: application/octet-stream
Authorization: Basic XXXXGHuIOIUO6hndhfhrjt
Connection: keep-alive
Content-Type: application/xml
Origin: chrome-extension: //rest-console-id
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36

响应正文

Request Url: https://testweb/test.apk?overwrite=true --data-binary @app0720.apk
Request Method: POST
Status Code: 200
Params: {}

我的java代码有什么问题。

最佳答案

由于这是一个 POST 调用,我相信添加以下语句应该可以解决问题。

urlConnection.setRequestMethod("POST");

关于java.io.IOException : Server returned HTTP response code: 403 for URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31846341/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com