HttpsURLConnection Failed on Second request - java

I am using HttpsURLConnection to connect client application. I am sending multiple requests for 1 user request(like login, dosearch and getResult).
It works fine as expected for First user's request(Server 1st request). but when for Second request code fails and thus facing multiple redirects.
When I start the local server for 1 request only it were working fine and remaining are failing and I have tried by below setting:
con.setRequestProperty("Connection", "close");
System.setProperty("https.proxyPort", "8888");
System.setProperty("http.keepAlive", "false");
I am able to establish the connection but the code which run for 1st time same code was failing there after.
Sample code:
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();
}
like this am making multiple requests and creating different HttpsURLConnection for each request.
Thanks for the help in advance.

Each HttpURLConnection instance is used to make a single request. If you want to send multiple request create a method getHttpURLConnection() which gives new connection every time.

I think you might need to use HttpsURLConnectionImpl.getOutputStream().close(). Then try another request.

Related

How does HTTP POST method transfer data ?

I know what is the HTTP header and how the HTTP data format , I also know how to make a HTTP post from Java ,
like
StringBuffer result = new StringBuffer();
PrintWriter out = null;
BufferedReader in = null;
StringBuffer result = new StringBuffer();
try {
URL realUrl = new URL("http://somesite/somepage.htm");
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
I can use some code like this write stream to web server , and read stream also .
Here is my question .
What is the HTTP POST method really do , I mean how does the client communicate to web server ?
What will happen if the web server only read the HTTP POST header and don't read the stream from client ?
Does the stream will stuck in somewhere ?
Thanks .

Sending post request to website

I'm trying to use java to log in to a website and then eventually retrieve the cookie in order to gain access to information on the site. It seems my post request is working but I am receiving a response code of 500. I was wondering if this is because I have formatted the post data incorrectly
When using the website it says the post is formatted as below
{userName: "dfh", password: "suyj"}
In my code I have used this
String urlParameters = "userName:dfh,Password:suyj";
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
Am I right in thinking this could be the issue, and if so how can I correct my post data format?
Below is my whole code
public class post {
public static void main(String[] args) throws IOException {
final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0";
String url = "website";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Accept-Encoding", "gzip, deflate");
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setRequestProperty("X-Requested-With", "XMLHttpRequest");
con.setRequestProperty("Referer", "https://extbasicph05.podc.sl.edst.ibm.com/FFAMobileRelay/");
con.setRequestProperty("Content-Length", "64");
con.setRequestProperty("Connection", "keep-alive");
con.setRequestProperty("Referer", "https://extbasicph05.podc.sl.edst.ibm.com/FFAMobileRelay/");
con.setRequestProperty("Cookies", "UnicaNIODID=KGew34gcvZ5-Y2NoBQr; mmid=-1658088866%7CKAAAAAr15Tc8FgsAAA%3D%3D; mmcore.pd=1484641984%7CejEyAAoBQvXlNzwWCxPPXGheAVibftSXgNJIDwAAADoOVvoDstFIAAAAAP//////////AAZEaXJlY3QBFgwIAAYABQAAAAAAAP+MAACAigAA/4wAAAIAxDEAAABFBVUW6QsA/////wHpCxoM//83AAAAAAAAAAOSfwAAusgAAJN/AAAgyAAAlH8AACHIAAABIYAAAAIAAADVNgAAAPcCB+70CwD/////AfQLHQz//60CAAEAAAAAAX+KAAAp2gAAAoCKAABAWBAAgYoAALMAAAAAAAABRQ%3D%3D; mmcore.srv=ldnvwcgus03; IBM_W3SSO_ACCESS=w3-03.sso.ibm.com; CoreID6=32675392371714128784599&ci=51040000|IBMTEST_51040000|IBMTESTW3_50200000|IBMTESTWWW; CoreM_State=75~-1~-1~-1~-1~3~3~5~3~3~7~7~|~~|~~|~~|~||||||~|~~|~~|~~|~~|~~|~~|~~|~; CoreM_State_Content=6~|~EE9EDB09923CD77F~|~0; PrefID=222-11978519; CSList=23427744/17540903,0/0,0/0,0/0,0/0; _ga=GA1.2.233924805.1433836377; mmcore.tst=0.169; ibmSurvey=1435758135373; 50200000_clogin=v=1&l=1435758136&e=1435760174616");
String urlParameters = "userName:dfh,Password:suyj";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
System.out.println(con.getErrorStream());
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
}
You have already found out your problem. Just fix it...
Instead of String urlParameters = "userName:dfh,Password:suyj";, use
String urlParameters = "{userName:\"dfh\",Password:\"suyj\"}";
Though I believe your server expects JSON formatted post parameter. In that case you should use,
String urlParameters = "{\"userName\":\"dfh\",\"Password\":\"suyj\"}";
Or use a JSON parser to create the parameters.

Java HttpURLConnection trying to login with cookie

So am trying to login this website using java but for some reason its not working as expected i got the host and all that stuff but its not going to the account page with the cookie it still shows the login page and yes my account info is correct any help is great
public static void main(String[] args) {
try {
String params = "loginEmail=private#hotmail.com&loginPassword=privatepassword&Submit=Sign+In";
String urls = "http://www.filefactory.com/member/signin.php";
URL url = new URL(urls);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Host", "www.filefactory.com");
connection.setRequestProperty("Referer", "http://www.filefactory.com/member/signin.php");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36 OPR/25.0.1614.50");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(params.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (params);
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuilder response = new StringBuilder();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
System.out.println(response.toString());
// get the cookie if need, for login
String cookies = connection.getHeaderField("Set-Cookie");
// open the new connnection again
connection = (HttpURLConnection) new URL("http://www.filefactory.com/account/").openConnection();
connection.setRequestProperty("Cookie", cookies);
connection.addRequestProperty("Accept-Language", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36 OPR/25.0.1614.50");
connection.addRequestProperty("Host", "www.filefactory.com");
System.out.println("Redirect to URL : " + "http://www.filefactory.com/account/");
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder html = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
html.append(inputLine);
}
in.close();
System.out.println("URL Content... \n" + html.toString());
System.out.println("Done");
} catch (Exception e) {
e.printStackTrace();
}
}
}
You are using : String cookies = connection.getHeaderField("Set-Cookie");
Are you sure there is only one entry for that header? There could be more.
http://en.wikipedia.org/wiki/HTTP_cookie
Try using chrome or firefox and try logging in manually to capture the request and response. That may give you some hints regarding what could be wrong.
Additionally you could use a tool to view the communication between your client and the server (unless you are already doing so)
It's hard to tell, not knowing the exact way that website works, but you should note that it sends you the cookies first when it presents the login page to you. When you send in your credentials you have to already send them together with the cookies, so that it knows to associate those credentials with this cookie.

Java HttpURLConnection no cookies at login

I'm currently trying to do a login with HttpURLConnection and then get the session cookies...
I already tried that on some test pages on my own server, and that works perfectly. When i send a=3&b=5 i get 8 as cookie (PHP page adds both together)!
But when i try that at the other page, the output is just the page as if I just sent nothing with POST :(
General suggestions for improvement are welcome! :)
My Code:
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("useragent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0");
conn.setRequestProperty("Connection", "keep-alive");
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes("USER=tennox&PASS=*****");
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
String response = new String();
while ((line = in.readLine()) != null) {
response = response + line + "\n";
}
in.close();
System.out.println("headers:");
int i = 0;
String header;
while ((header = conn.getHeaderField(i)) != null) {
String key = conn.getHeaderFieldKey(i);
System.out.println(((key == null) ? "" : key + ": ") + header);
i++;
}
String cookies = conn.getHeaderField("Set-Cookie");
System.out.println("\nCookies: \"" + cookies + "\"");
The cookie path should initially be set with ; Path=/, also needing a Set-Cookie in the request header of the POST.
Better rewrite it all with an HttpClient.

Java HttpURLConnection no cookies

English:
Hello, I'm currently trying to do a login with HttpURLConnection and then get the session cookies...
I already tried that on some test pages on my own server, and that works perfectly. When i send "a=3&b=5" i get "8" as cookie (PHP page adds both together)!
But when i try that at the other page, the output is just the page as if I just sent nothing with POST :(
General suggestions for improvement are welcome! :)
My Code:
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("useragent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0");
conn.setRequestProperty("Connection", "keep-alive");
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes("USER=tennox&PASS=*****");
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
String response = new String();
while ((line = in.readLine()) != null) {
response = response + line + "\n";
}
in.close();
System.out.println("headers:");
int i = 0;
String header;
while ((header = conn.getHeaderField(i)) != null) {
String key = conn.getHeaderFieldKey(i);
System.out.println(((key == null) ? "" : key + ": ") + header);
i++;
}
String cookies = conn.getHeaderField("Set-Cookie");
System.out.println("\nCookies: \"" + cookies + "\"");
I see the following possibilities why you don't get a cookie.
server does NOT send cookies at all
server does not use the values PASS and USER
server want to have a session before login.
POST protocol is wrong or incomplete
server expects parameters by GET

Categories