For the last two hours i’ve been trying to make a POST request to this page http://www.halebop.se/butik/byt_behall_nummer/ and tried to send numberToPort. However i get a bunch of cookies and a 302 moved temporarily back.
All i want to do is send the POST request with the number and get the final page back. On iOS, i do this using ASIHTTPRequest which handles the redirect and cookies.
iOS code:
NSString *halebopURLString = @"http://www.halebop.se/kontantkort/byt_behall_nummer/#";
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:halebopURLString]];
[request setPostValue:halebopNumber forKey:@"numberToPort"];
[request setPostValue:@"continue" forKey:@"action"];
[request setPostValue:@"submit" forKey:@"submit"];
[request startSynchronous];
How do i do this on Android?
As an alternative, a PHP solution is acceptable.
Edit: Tried this, it gives no output and no exceptions. I have the internet permission. Expected result: Send POST, get 302 and cookies back, send cookies to URL from 302 and get HTML back (Checked with FireBug) however i get nothing.
try {
InputStream myInputStream =null;
URL url;
url = new URL("http://www.halebop.se/kontantkort/byt_behall_nummer/#");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(true);
conn.setRequestMethod("POST");
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write("numberToPort="+n+"&action=continue&submit=submit");
wr.flush();
myInputStream = conn.getInputStream();
wr.close();
BufferedReader rd = new BufferedReader(new InputStreamReader(myInputStream), 4096);
String line;
StringBuilder sbResult = new StringBuilder();
while ((line = rd.readLine()) != null) {
sbResult.append(line);
Log.d(TAG, "Line "+line);
}
rd.close();
String contentOfMyInputStream = sbResult.toString();
Log.d(TAG, "Output "+contentOfMyInputStream);
} catch (Exception e) {
Log.d(TAG,e.getMessage());
}
Here is how you can set the post parameters:
Here is the detailed explanation about the code.
I also explained How to retrieve HTTP cookies from your response and set them into request here