gpt4 book ai didi

java - HttpsURLConnection 第二次请求失败

转载 作者:行者123 更新时间:2023-12-02 11:46:30 26 4
gpt4 key购买 nike

我正在使用 HttpsURLConnection 连接客户端应用程序。我正在为 1 个用户请求发送多个请求(例如登录、dosearch 和 getResult)。

对于第一个用户的请求(服务器第一个请求),它工作正常。但是当第二个请求代码失败并因此面临多个重定向时。

当我为 1 个请求启动本地服务器时,它工作正常,其余的则失败,我已尝试通过以下设置:

con.setRequestProperty("Connection", "close");
System.setProperty("https.proxyPort", "8888");
System.setProperty("http.keepAlive", "false");

我能够建立连接,但第一次运行相同代码的代码随后失败。

示例代码:

URL url2 = new URL("client url");
HttpsURLConnection con2 = (HttpsURLConnection) url2.openConnection();
con2.setSSLSocketFactory(sc.getSocketFactory());

CookieHandler.setDefault(new CookieManager());
con2.setUseCaches(false);
con2.setDefaultUseCaches(false);
con2.setDoOutput(true);
con2.setDoInput(true);
con2.setReadTimeout(5000);
con2.setRequestMethod("POST");
con2.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0");
con2.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
con2.setRequestProperty("Accept-Language","en-US,en;q=0.5");
con2.setRequestProperty("Cache-Control","max-age=0");
con2.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con2.setReadTimeout(6000);
con2.setConnectTimeout(10000);
con2.setRequestProperty("Connection", "close");

List<NameValuePair> nameValuePairsLogin = new ArrayList<NameValuePair>();

nameValuePairsLogin.add(new BasicNameValuePair("username", "username"));
nameValuePairsLogin.add(new BasicNameValuePair("password", "password"));
// some other inputs

String params=Utils.getQuery(nameValuePairsLogin).toString();
byte[] postDataBytes = params.toString().getBytes("UTF-8");
con2.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
con2.setInstanceFollowRedirects(false);

DataOutputStream wr = new DataOutputStream(con2.getOutputStream());
wr.write(postDataBytes);
wr.flush();
wr.close();

boolean redirect = false;
int status = con2.getResponseCode();

if (status != HttpsURLConnection.HTTP_OK) {
if (status == HttpsURLConnection.HTTP_MOVED_TEMP
|| status == HttpsURLConnection.HTTP_MOVED_PERM
|| status == HttpsURLConnection.HTTP_SEE_OTHER)
redirect = true;
}

System.out.println("Response Code ... " + status);
if (redirect) {
String newUrl = con2.getHeaderField("Location");
String cookies = con2.getHeaderField("Set-Cookie");

// open the new connnection again
con2 = (HttpsURLConnection) new URL(newUrl).openConnection();
con2.setSSLSocketFactory(sc.getSocketFactory());
con2.setRequestProperty("Cookie", cookies);
con2.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0");
con2.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
con2.setRequestProperty("Accept-Language","en-US,en;q=0.5");
//con2.setRequestProperty("Connection","keep-alive");
con2.setRequestProperty("Cache-Control","max-age=0");
con2.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con2.addRequestProperty("Referer", "Client URL");
con2.setReadTimeout(6000);
con2.setConnectTimeout(10000);
con2.setRequestProperty("Connection", "close");
con2.setUseCaches(false);
con2.setDoOutput(true);
con2.setDoInput(true);
con2.setDefaultUseCaches(false);

System.out.println("Redirect to URL : " + newUrl);
DataOutputStream wr1 = new DataOutputStream(con2.getOutputStream());
wr1.write(postDataBytes);
wr1.flush();
wr1.close();
}



String resonse2=Utils.toString(con2.getInputStream());

Utils.toString:

public static String toString(InputStream inputStream) throws IOException {


String inputLine;
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
in=null;
return response.toString();
}

像这样发出多个请求并为每个请求创建不同的 HttpsURLConnection。

感谢您提前提供的帮助。

最佳答案

每个HttpURLConnection实例用于发出单个请求。如果您想发送多个请求,请创建一个方法 getHttpURLConnection() ,该方法每次都会提供新的连接。

关于java - HttpsURLConnection 第二次请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48162456/

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