gpt4 book ai didi

java - 服务器返回 HTTP 响应代码 : 403 for URL:(How do I fix this? )

转载 作者:行者123 更新时间:2023-12-01 16:35:33 26 4
gpt4 key购买 nike

当我运行下面的代码时,我不断收到上述错误。从我读到的内容来看,所有迹象都表明 COOKIES 存在问题。如果我是正确的,我将如何实现 CookieManager 来解决这个问题?或者,如果不是 COOKIES 的问题,我该如何解决该问题?

public class Client {

public Client(){
}

String executePost(String targetURL, String urlParameters){
URL url;
HttpURLConnection connection = null;

try{
//Create connection
url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setChunkedStreamingMode(0);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");

connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);

//send Request
DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
dataout.writeBytes(urlParameters);
dataout.flush();
dataout.close();

//get response
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();

while((line = br.readLine()) != null){
response.append(line);
response.append('\n');

}
System.out.println(response.toString());
br.close();
return response.toString();
}catch(Exception e){
System.out.println("Unable to full create connection");
e.printStackTrace();
return null;
}finally {

if(connection != null) {
connection.disconnect();
}
}
}
}

最佳答案

我删除了:connection.setChunkedStreamingMode(0);并且代码按其应有的方式工作

String executePost(String targetURL, String urlParameters){

URL url;
HttpURLConnection connection = null;
try{
//Create connection

url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();

connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Language", "en-US");
connection.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11");

connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);

//send Request
DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
dataout.writeBytes(urlParameters);
dataout.flush();
dataout.close();

//get response
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();

while((line = br.readLine()) != null){
response.append(line);
response.append('\n');

}
System.out.println(response.toString());
br.close();
return response.toString();
}catch(Exception e){
System.out.println("Unable to full create connection");
e.printStackTrace();
return null;
}finally {

if(connection != null) {
connection.disconnect();
}
}


}

关于java - 服务器返回 HTTP 响应代码 : 403 for URL:(How do I fix this? ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9553823/

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