Post Request clone on Android - java

Im trying to perform the folowing request in my android app.
The Request i send was on a Chrome Browser.
In the Request i shortend the Form Data so there is not so much code.
Request URL:http://***/
Request Method:POST
Status Code:200 OK
Request Headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:424
Content-Type:application/x-www-form-urlencoded
Cookie:__utma=188893489.1646114392.1358703936.1367178892.1368783485.29; __utmz=188893489.1365594840.21.3.utmcsr=***|utmccn=(referral)|utmcmd=referral|utmcct=/
Host:***
Origin:***
Referer:**
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
Form Data
date=1375480155&mail=&dfBoot=Test
Response Headers
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:3614
Content-Type:text/html
Date:Fri, 02 Aug 2013 21:49:59 GMT
Keep-Alive:timeout=15, max=100
Server:Apache/2.2.14 (Ubuntu)
Vary:Accept-Encoding
X-Powered-By:PHP/5.3.2-1ubuntu4.20
with this code:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
URL url = new URL("http://***/index.php");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length",String.valueOf(post.length()));
//connection.setChunkedStreamingMode(0);//results in frezing
OutputStreamWriter writer = new OutputStreamWriter(
connection.getOutputStream());
writer.write(post);
writer.flush();
InputStream in = new BufferedInputStream(
connection.getInputStream());
while (in.available() != 0) {
in.read();
}
writer.close();
in.close();
} finally {
connection.disconnect();
}
The code is very messed up because i tryed manytime to fix my connection problem.
Please help with a better Solution.

Hm, I will show my working code with JSON, but maybe you can modify it.
URL url = new URL("http://xcxcxcxcxcx");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(jsonObject.toString().getBytes().length));
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(jsonObject.toString());
out.flush();
out.close();
InputStream is = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(is);
BufferedReader r = new BufferedReader(reader);
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
JSONObject jsonObjectResult = null;
if(typeOfMethod == 0){
JSONTokener tokenizer = new JSONTokener(total.toString());
jsonObjectResult = new JSONObject(tokenizer);
}
connection.disconnect();
return jsonObjectResult;
There is some differences between getting result from InputStream.

Related

HttpURLConnection with https InputStream Garbled

I use HttpURLConnection to crawler https://translate.google.com/.
InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 1082);
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
url = new URL("https://translate.google.com/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
conn.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("User-Agent",
"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36");
conn.setRequestProperty("Accept", "*/*");
Map<String, List<String>> reqHeaders = conn.getHeaderFields();
List<String> reqTypes = reqHeaders.get("Content-Type");
for (String ss : reqTypes) {
System.out.println(ss);
}
InputStream in = conn.getInputStream();
String s = IOUtils.toString(in, "UTF-8");
System.out.println(s.substring(0, 100));
Map<String, List<String>> resHeader = conn.getHeaderFields();
List<String> resTypes = resHeader.get("Content-Type");
for (String ss : resTypes) {
System.out.println(ss);
}
Console is
But When I change url to http://translate.google.com/.
It works well.
I know actually HttpURLConnection is HttpsURLConnection when i crawler https://translate.google.com/.
I try to use HttpsURLConnection and it still garbled.
Any suggestions?
conn.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
The response is compressed, because the above line tells the server that the client is able to understand encodings specified in Accept-Encoding.
Try to comment this line or handle this situation.
There's a more specific implementation for HTTPS i.e. HttpsURLConnection, in case you're interested in https-specific features, e.g.:
import javax.net.ssl.HttpsURLConnection;
....
URL url = new URL("https://www.google.com/");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
I accept Jerry Chin's answer.Solves my problem.
My answer just recording how i resolve this problem.
If this approach is unreasonable.Let me know, I'll remove this answer.
conn.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
And then I check response Content-Encoding.It's gzip.
So i use GZIPInputStream to receive.
InputStream in = conn.getInputStream();
GZIPInputStream gzis=new GZIPInputStream(in);
InputStreamReader reader = new InputStreamReader(gzis);
BufferedReader br = new BufferedReader(reader);
The InputStream is normal.
BTW,If you don't need Accept-Encoding,you can remove it.
And do not forget check user-agent. It's very important and different operating systems corresponding to different user-agent.

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 :-(

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.

java getting incomplete soap response

In my project I am calling a web service by the sending soap request in this format,
try {
URL u = new URL(server);
URLConnection uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("SOAPAction", SOAP_ACTION);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-type", "text/xml; charset=utf-8");
String xmldata = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
"<soap:Body>"+
"<GetAllCars xmlns=\"http://parkinghero.com/\">"+
"<clsGetAllCarsRequest>"+
"<clsCredentials>"+
"<EmailAddress>"+Email+"</EmailAddress>"+
"<Password>"+passWRD+"</Password>"+
"<TokenID>"+token+"</TokenID>"+
"</clsCredentials>"+
"</clsGetAllCarsRequest>"+
"</GetAllCars>"+"</soap:Body>"+
"</soap:Envelope>";
System.out.println(xmldata);
OutputStream out = connection.getOutputStream();
Writer wout = new OutputStreamWriter(out);
wout.write(xmldata);
wout.flush();
wout.close();
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//InputStream in = connection.getInputStream();
String result;
//int c;
while ((result=rd.readLine()) != null) {
System.out.println(result);
int length = result.length();
String jsonObj = result.substring(294, (length - 74));
System.out.println(jsonObj);
}rd.close();
}
catch (IOException e)
{
System.out.println("in Exception called");
System.err.println(e);
}
For this request the response will be json array
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetAllCarsResponse xmlns="http://abcd.com/">
<GetAllCarsResult>
{"clsError":{"ErrorCode":306,"ErrorDescription":"Data Available"},
"CarsArray":[{"CarID":1,"UserID":1,"RegisteredOwner":"Suresh thorata","Make":"Esteem","Model":"23434","Color":"Brownd","BodyType":"Normal","PlateType":"COM",
"PlateNumber":"WAG1PM","State":"NY","VINNumber":"000000000000","YearOfRegistration":2000,"ExpiryDate":"10/25/2014 4:24:57 PM",
"IsDeleted":true,"DateAddedTimeStamp":"10/18/2010 10:19:04 AM","LastUpdatedTimeStamp":"10/25/2010 4:24:57 PM"},
{"CarID":2,"UserID":1,"RegisteredOwner":"Balaji","Make":"Land Rover","Model":"9999","Color":"Brown","BodyType":"Normal","PlateType":"PAS","PlateNumber":"WAG2PM",
"State":"NY","VINNumber":"","YearOfRegistration":2003,"ExpiryDate":"10/18/2014 11:01:07 AM","IsAutoFightOn":true,"IsDeleted":true,"DateAddedTimeStamp":"10/18/2010
11:01:07 AM","LastUpdatedTimeStamp":"10/18/2010 11:01:07 AM"},
{"CarID":3,"UserID":1,"RegisteredOwner":"Suresh","Make":"Dodge","Model":"5670","Color":"Brown","BodyType":"Normal","PlateType":"AYG","PlateNumber":"WAG3PM",
"State":"NY","VINNumber":"","YearOfRegistration":2000,"ExpiryDate":"10/18/2014 11:01:39 AM","IsDeleted":true,"DateAddedTimeStamp":"10/18/201
0 11:01:39 AM","LastUpdatedTimeStamp":"12/9/2010 11:36:14 PM"},
{"CarID":4,"UserID":1,"RegisteredOwner":"Suresh Babu","Make":"Infiniti","Model":"234353","Color":"Brownd","BodyType":"Normal","PlateType":"PAS",
"PlateNumber":"WAG4PM","State":"NY","VINNumber":"000000000000","YearOfRegistration":2000,"ExpiryDate":"10/20/2014 1:01:22 PM","IsAutoFightOn":true,
"IsDeleted":true,"DateAddedTimeStamp":"10/20/2010 1:01:22 PM","LastUpdatedTimeStamp":"12/3/2010 12:20:29 PM"},{"CarID":5,"UserID":1,
"RegisteredOwner":"Suresh1 thorata","Make":"Esteem","Model":"23434","Color":"Brownd","BodyType":"Normal","PlateType":"COM",
"PlateNumber":"WAG5PM","State":"NY","VINNumber":"000000000000","YearOfRegistration":2000,"ExpiryDate":"10/25/2014 11:44:06 AM"
,"IsDeleted":true,"DateAddedTimeStamp":"10/25/2010 11:39:00 AM","LastUpdatedTimeStamp":"10/25/2010 11:44:06 AM"},{"CarID":73,"UserID":1,
"RegisteredOwner":"Balaji ","Make":"Audi","Model":"Sportz","Color":"Red","BodyType":"Coupe","PlateType":"PAS",
"PlateNumber":"SUPERMAN","State":"NJ","VINNumber":"","YearOfRegistration":2010,"ExpiryDate":"12/12/2020 12:00:00 AM",
"IsAutoFightOn":true,"IsDeleted":true,"DateAddedTimeStamp":"10/25/2010 6:36:07 PM","LastUpdatedTimeStamp":"12/9/2010 11:41:46 PM"},
{"CarID":74,"UserID":1,"RegisteredOwner":"suresh","Make":"BMW","Model":"sedane","Color":"silver","BodyType":"Convertible","PlateType":"PAS",
"PlateNumber":"GREENMAN","State":"NJ","VINNumber":"","YearOfRegistration":2010,"ExpiryDate":"12/12/2020 12:00:00 AM","IsAutoFightOn":true,"IsDeleted":false,
"DateAddedTimeStamp":"10/26/2010 10:15:34 AM","LastUpdatedTimeStamp":"12/9/2010 11:38:56 PM"},{"CarID":79,"UserID":1,"RegisteredOwner":"Balu","Make":"Hero",
"Model":"Honda","Color":"Green","BodyType":"2005","PlateType":"ARG - Air National Guard","PlateNumber":"2434","State":"NY","VINNumber":"12345",
"YearOfRegistration":2004,"ExpiryDate":"3/17/2021 12:00:00 AM","IsAutoFightOn":false,"IsDeleted":false,"DateAddedTimeStamp":"11/17/2010 9:55:32 AM",
"LastUpdatedTimeStamp":"12/6/2010 11:13:01 AM"},{"CarID":80,"UserID":1,"RegisteredOwner":"Balaji","Make":"Accura","Model":"i10","Color":"Red",
"BodyType":"1994","PlateType":"ATD - All Terrain Dealer","PlateNumber":"NOFEAR","State":"DE","VINNumber":"456789","YearOfRegistration":1999,
"ExpiryDate":"10/29/2047 12:00:00 AM","IsAutoFightOn":true,"IsDeleted":false,"DateAddedTimeStamp":"11/17/2010 11:25:54 AM",
"LastUpdatedTimeStamp":"11/17/2010 11:25:54 AM"},{"CarID":81,"UserID":1,"RegisteredOwner":"Balaji","Make":"Esteem","Model":"Honda","Color":"LemonYellow",
"BodyType":"1976","PlateType":"AYG","P
I am able to get response but it is incomplete. what am i doing wrong. I think it is because of buffer reader.
#Kishore: here's the code
String SOAP_ACTION = "service soap action";
try {
URL u = new URL("server url");
URLConnection uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("SOAPAction", SOAP_ACTION);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-type", "text/xml; charset=utf-8");
String xmldata="soap request envelope";
//send the request
OutputStream out = connection.getOutputStream();
Writer wout = new OutputStreamWriter(out);
wout.write(xmldata);
wout.flush();
wout.close();
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String result;
StringBuilder builder=new StringBuilder();
//read response
while ((result=rd.readLine()) != null) {
builder.append(result);
}
thanks for your help

Categories