HTTPSUrlConnection - How to see headers sent on Microsoft Windows - java

I've this code in Java 8
String u = "https://tes.com/Prova.asmx?WSDL";
URL url = new URL(u);
System.out.println("Link: " + u);
SSLContext sc = SSLContext.getInstance("TLSv1");
sc.init(null, null, new java.security.SecureRandom());
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setSSLSocketFactory(sc.getSocketFactory());
System.out.println("After url connection");
//con.setRequestProperty("Accept","*/*");
//con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
//con.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), Charset.forName("UTF-8")));
String input;
while ((input = br.readLine()) != null) {
System.out.println(input);
}
br.close();
How can i see header that are sent on connection using Microsoft Windows?
Thanks

Related

HttpsURLConnection Failed on Second request

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.

Java send request encoding

I'm trying to send get request in java, when it's without headers as below, I get redirected to login page.
URL myURL1 = new URL("http://www.eventlister.com");
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
But when request is sent with all headers as it is sent from browser when reading response I get it encoded like this
???}isI??g)b?CZ?n?7BH?:m?u??dkzz; $P????C2?????YT??;??B3m????7/?.o~?p,?????..., even if encoding is set manually in reader.
URL myURL1 = new URL("http://www.eventlister.com");
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36");
con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
con.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,ru;q=0.2,uk;q=0.2,ja;q=0.2,fr-FR;q=0.2,fr;q=0.2");
con.setRequestProperty("Connection", "keep-alive");
con.setRequestProperty("Upgrade-Insecure-Requests", "1");
con.setRequestProperty("Host", "www.eventlister.com");
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
Thank you in advance.
It looks like it's gzip'ed data. Try it without "Accept-Encoding: gzip", so that server returns plain data.
I know this is a comment, not an answer. I don't have comment privilege yet :-(

Get page content from URL in java

Not able to access content of this page "kissanime.com" (it's not returning anything) from URL by this code :
String a="http://kissanime.com";
url = new URL(a);
URLConnection conn = url.openConnection();
try ( // open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()))) {
String inputLine;
while ((inputLine = br.readLine()) != null) {
System.out.println(inputLine);
}
}
As my above comment , you need to set user agent header by setRequestProperty method as below.
String a = "http://kissanime.com";
URLConnection connection = new URL(a).openConnection();
connection
.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
connection.connect();
BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream(),
Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
sb.append(line);
}
System.out.println(sb.toString());
Now you will get somethings !!

How to decrypt HttpsURLConnection

My code makes POST request to a website, but the response looks like encrypted, with weird characters. When i configure my app to use fiddler proxy, the response is valid. How can i make my app decrypt this response?
public class Login {
public Login() throws Exception {
URL url = new URL("https://account.leagueoflegends.com/auth");
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
conn.setDoInput(true);
conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36");
conn.addRequestProperty("Cookie", "xxx");
conn.addRequestProperty("Content-Length", "406");
conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
conn.addRequestProperty("Accept-Encoding", "gzip,deflate");
conn.addRequestProperty("Connection", "keep-alive");
conn.addRequestProperty("Referer", "xxx");
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write("xxx");
writer.flush();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
writer.close();
reader.close();
}
You are explicitly requesting a zipped stream:
conn.addRequestProperty("Accept-Encoding", "gzip,deflate");
Remove that line to remove the compression. Compression is different from encryption fortunately, it doesn't require you to supply a key.

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.

Categories