我正在使用HttpsURLConnection
来连接客户端应用程序。我发送了多个1个用户请求的请求(如login,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。
感谢您的帮助。
答案 0 :(得分:1)
每个HttpURLConnection
实例用于发出单个请求。如果要发送多个请求,请创建一个方法getHttpURLConnection()
,每次都会提供新连接。
答案 1 :(得分:0)
我认为您可能需要使用HttpsURLConnectionImpl.getOutputStream()。close()。然后尝试另一个请求。