Android上的HTTP POST请求Android、HTTP、POST

2023-09-07 11:55:29 作者:手持AK47大喊德玛西亚

在过去的两小时,我一直试图做一个POST请求这个网页。的http:/ /www.halebop.se/butik/byt_behall_nummer/ 并试图向其发送的 numberToPort 。但是我得到了一堆饼干和一个302移动暂时回来了。

所有我想要做的就是发送一个带有编号的POST请求,并获得最后一页回来。在iOS上,我做到这一点使用ASIHTT prequest它处理重定向和饼干。

的iOS code:

 的NSString * halebopURLString = @http://www.halebop.se/kontantkort/byt_behall_nummer/#ASIFormDataRequest *请求= [ASIFormDataRequest requestWithURL:[NSURL URLWithString:halebopURLString]];[要求setPostValue:halebopNumber forKey:@numberToPort];[要求setPostValue:@继续forKey:@行动];[要求setPostValue:@提交forKey:@提交];[请求startSynchronous] 

我怎么做这在Android?

作为替代方案,一个PHP的解决方案是可接受的。

编辑:试过,它没有给输出及无异常。我有上网权限。预期结果:发送POST,获得302和饼干回来,发送cookie从302到URL并获得HTML背面(经过与萤火虫),但是我什么也得不到。

  {尝试        InputStream的myInputStream = NULL;        URL网址;        URL =新的URL(http://www.halebop.se/kontantkort/byt_behall_nummer/#);        HttpURLConnection的康恩=(HttpURLConnection类)url.openConnection();        conn.setDoOutput(真);        conn.setInstanceFollowRedirects(真);        conn.setRequestMethod(POST);        OutputStreamWriter WR =新OutputStreamWriter(conn.getOutputStream());        wr.write(numberToPort =+ N +&放大器;行动=继续和放大器;提交=提交);        wr.flush();        myInputStream = conn.getInputStream();        wr.close();        RD的BufferedReader =新的BufferedReader(新的InputStreamReader(myInputStream),4096);        串线;        StringBuilder的sbResult =新的StringBuilder();        而((行= rd.readLine())!= NULL){            sbResult.append(线);            Log.d(TAG,线+线);        }        rd.close();        串contentOfMyInputStream = sbResult.toString();        Log.d(TAG,输出+ contentOfMyInputStream);    }赶上(例外五){        Log.d(TAG,e.getMessage());    } 

解决方案 Android HTTPPOST请求报错

下面是如何设置岗位参数:

  HttpPost httpost =新HttpPost(LOGIN_URL);    清单<&的NameValuePair GT; nvps =新的ArrayList<&的NameValuePair GT;();    nvps.add(新BasicNameValuePair(测试1,测试1));    nvps.add(新BasicNameValuePair(test2的,测试2));            httpost.setEntity(新UrlEn codedFormEntity(nvps,HTTP.UTF_8));    响应= GETRESPONSE(httpost); 

这里是详细解释关于code。

我还解释如何从你的回答中检索的HTTP cookies,并将它们设置成要求的这里

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:

            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