Android HTTP Post授权请求

奥柳丁

我正在尝试在Android应用程序中发送HTTP Post请求。

这是我应该发送的: 在此处输入图片说明 在此处输入图片说明

这是我应该收到的:

在此处输入图片说明

因此,总而言之,我需要从响应中获取Cookie,并且响应代码应为302。

我有以下Android代码:

 private static String makePostRequest() {

    HttpClient httpClient = new DefaultHttpClient();
                            // replace with your url
    HttpPost httpPost = new HttpPost("http://g1.botva.ru/login.php"); 

    boolean flag = httpClient.getParams().isParameterTrue(ClientPNames.HANDLE_REDIRECTS);

    httpPost.addHeader("Accept", "*/*");
    httpPost.addHeader("Accept-Encoding", "gzip, deflate");
    //httpPost.addHeader("Content-Length", "83");
    httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
    httpPost.addHeader("User-Agent", "runscope/0.1");

    Header [] arr = httpPost.getAllHeaders();

    String result123 = "";
    //Post Data
    List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(5);

    nameValuePair.add(new BasicNameValuePair("do_cmd", "login"));
    nameValuePair.add(new BasicNameValuePair("server", "1"));
    nameValuePair.add(new BasicNameValuePair("email", "[email protected]"));
    nameValuePair.add(new BasicNameValuePair("password", "avmalyutin1234"));
    nameValuePair.add(new BasicNameValuePair("remember", "1"));



    //Encoding POST data
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
    } catch (UnsupportedEncodingException e) {
        // log exception
        e.printStackTrace();
    }

    //making POST request.
    try {
        HttpResponse response = httpClient.execute(httpPost);
        String getCookie = response.getHeaders("Pragma").length + "";
        result123 = response.getStatusLine().getStatusCode()+"";
        // write response to log
        Log.d("Http Post Response:", response.toString());
    } catch (ClientProtocolException e) {
        // Log exception
        e.printStackTrace();
    } catch (IOException e) {
        // Log exception
        e.printStackTrace();
    }

    return result123;
}

而且我收到代码200。Cookies中只有PHPSESSIONID,但没有其他Cookie。

奥柳丁

增加功能

con.setInstanceFollowRedirects(false);

解决该问题,它将停止重定向。而且我也收到了所需的代码302。

谢谢大家的帮助

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章