Android 上的 HTTP POST 请求

发布于 2024-12-01 01:03:55 字数 2031 浏览 0 评论 0原文

在过去的两个小时里,我一直在尝试向此页面发出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天赋异禀 2024-12-08 01:03:55

以下是设置帖子参数的方法:

            HttpPost httpost = new HttpPost(LOGIN_URL);
    List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("test1","test1" ));
    nvps.add(new BasicNameValuePair("test2", "test2" ));

            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

    response = getResponse(httpost);

此处是关于代码的详细解释。

我还解释了如何从响应中检索 HTTP cookie 并将其设置为请求 这里

Here is how you can set the post parameters:

            HttpPost httpost = new HttpPost(LOGIN_URL);
    List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("test1","test1" ));
    nvps.add(new BasicNameValuePair("test2", "test2" ));

            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

    response = getResponse(httpost);

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

孤独患者 2024-12-08 01:03:55

如果您使用 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 use setInstanceFollowRedirects(true) since blindly following redirects (what the static setFollowRedirects would cause) is frowned upon from a security perspective.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文