Android 上的 HTTP POST 请求
在过去的两个小时里,我一直在尝试向此页面发出 POST 请求 http://www. halebop.se/butik/byt_behall_nummer/ 并尝试发送numberToPort。不过我得到了一堆饼干和一辆 302 暂时搬回来。
我想做的就是发送带有号码的 POST 请求并获取最终页面。在 iOS 上,我使用 ASIHTTPRequest 来处理重定向和 cookies。
iOS 代码:
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];
如何在 Android 上执行此操作?
作为替代方案,PHP 解决方案也是可以接受的。
编辑:尝试过这个,它没有给出任何输出,也没有例外。我有互联网许可。预期结果:发送 POST,获取 302 和 cookie,将 cookie 从 302 发送到 URL,并获取 HTML(用 FireBug 检查),但是我什么也没得到。
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());
}
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());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是设置帖子参数的方法:
此处是关于代码的详细解释。
我还解释了如何从响应中检索 HTTP cookie 并将其设置为请求 这里
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
如果您使用 HttpUrlConnection,则 HttpUrlConnection#setFollowRedirects 可能就是你所追求的。将其设置为
true
以使其自动解析重定向。更好的是使用 setInstanceFollowRedirects(true) ,因为从安全角度来看,盲目跟随重定向(静态 setFollowRedirects 会导致的情况)是不受欢迎的。If you are using HttpUrlConnection, then HttpUrlConnection#setFollowRedirects might be what you are after. Set it to
true
to make it automatically resolve the redirect. Even better to usesetInstanceFollowRedirects(true)
since blindly following redirects (what the staticsetFollowRedirects
would cause) is frowned upon from a security perspective.