【问题标题】:Android HTTP Post authorization requestAndroid HTTP Post 授权请求
【发布时间】:2015-03-08 19:17:13
【问题描述】:

我正在尝试在 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", "avmalyutin@mail.ru"));
    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。

【问题讨论】:

  • 嘿 CodeMonkey,因为你已经在使用Runscope——你可能想查看documentation around Android。多个不同库的大量示例,以及有关如何通过您自己的捕获 URL 传递请求的提示,以便您可以在 Runscope Traffic Inspector 中检查 Android 客户端进行的 API 调用。

标签: android post cookies httprequest


【解决方案1】:

添加功能

con.setInstanceFollowRedirects(false);

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

感谢大家的帮助

【讨论】:

    【解决方案2】:

    您需要检测 DefaultHttpClient 以便您可以看到 WIRE 和 HEADERS。 Google 为您正在使用的客户端记录 WIRE HEADERS。如果您无法弄清楚日志记录调试(您可能要考虑DIFF client

    然后,比较执行帖子的 2 个框架(您的测试工具 VS android),只是缩小了正在发送的标头 + POST BODY 之间的差异。当您让 android 与您的测试工具融合时,android 将产生与您报告从测试工具获得的相同的 302 结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-26
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 2012-07-10
      • 2021-10-17
      • 2021-04-26
      • 1970-01-01
      相关资源
      最近更新 更多