【问题标题】:Android HTTP Post Request doesn't work, but stock Java does?Android HTTP Post 请求不起作用,但股票 Java 可以吗?
【发布时间】:2012-04-05 15:19:23
【问题描述】:

所以我有 2 个代码应该做同样的事情。然而,我在 Android 上使用的那个返回错误的 HTML 数据。股票 Java 一在发送请求后返回正确的数据。我这里有两个代码。你能告诉我(即使我给了 ANDROID 互联网许可)为什么 Android 不工作,而股票 Java 工作?这是安卓代码:

编辑:我找到了修复。如果要使用 StringEntity 将这样的字符串发送到服务器,则必须将内容设置为 application/x-www-form-urlencoded。我已经编辑了我的代码以显示这一点:

public static String sendNamePostRequest(String urlString) {

    HttpClient client = new DefaultHttpClient();

    HttpPost post = new HttpPost(urlString);

    StringBuffer sb = new StringBuffer();

    try {
           StringEntity se = new StringEntity(
                "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwULLTE3NDM5MzMwMzRkZA%3D%3D&__EVENTVALIDATION=%2FwEWBAL%2B%2B4CfBgK52%2BLYCQK1gpH7BAL0w%2FPHAQ%3D%3D&_nameTextBox=John&_zoekButton=Zoek&numberOfLettersField=3"); 

        se.setContent("application/x-www-form-urlencoded");

        post.setEntity();

        HttpResponse response = client.execute(post);
        HttpEntity entity = response.getEntity();

        BufferedReader br = new BufferedReader(new InputStreamReader(
                entity.getContent()));
        String in = "";

        while ((in = br.readLine()) != null) {
            sb.append(in + "\n");
        }

        br.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return sb.toString();
}

这是 Java 代码:

public String sendNamePostRequest(String urlString) {

    StringBuffer sb = null;

    try {
        String data = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwULLTE3NDM5MzMwMzRkZA%3D%3D&__EVENTVALIDATION=%2FwEWBAL%2B%2B4CfBgK52%2BLYCQK1gpH7BAL0w%2FPHAQ%3D%3D&_nameTextBox=John&_zoekButton=Zoek&numberOfLettersField=3";

        // String data = "";

        URL requestUrl = new URL(urlString);

        HttpURLConnection conn = (HttpURLConnection) requestUrl
                .openConnection();

        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");

        DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

        dos.writeBytes(data);
        dos.flush();

        BufferedReader br = new BufferedReader(new InputStreamReader(
                conn.getInputStream()));

        String in = "";
        sb = new StringBuffer();

        while ((in = br.readLine()) != null) {
            sb.append(in + "\n");
        }

        dos.close();
        br.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}

【问题讨论】:

  • 指定“错误的 HTML 数据”是什么意思
  • 这意味着我应该在它处理完请求后取回页面的HTML数据。普通的 Java 代码可以完美地做到这一点,但 Android 代码只是返回站点的 HTML 页面,而不考虑 POST 请求。 Wireshark 确实看到了来自 Android 和 Stock 方面的 POST,具有完全相同的数据字符串,但由于某种原因,他从 Java 发送的一个没有得到处理......
  • 在 android 案例中,您可能需要告诉 StringEntity 将 content-type 设置为 application/x-www-form-urlencoded
  • 我能想到你的实体字符串很麻烦。尝试使用new StringEntity("yourString", HTTP.UTF_8)

标签: java android http post request


【解决方案1】:

如果数据正在到达服务器,您可能想看看那里发生了什么(日志、错误、异常等)。除此之外:

  • 使用可以使用HttpURLConnection,所以你可以拥有完全相同的代码
  • 对于 HttpClient,不确定您自己是否在对实体进行编码。使用NameValuePair 设置参数,HttpClient 将为您(正确)编码。

【讨论】:

  • 对字符串进行编码只会在 Android 中出现问题。它说它不是base64(可能是因为代码中的_和%之类的东西。所以我不能对糟糕的字符串进行urlencode,但我不知道这是否真的是问题,因为它没有在股票 Java 中编码...
  • 您确实发布了您的“普通 Java”代码和 Android 代码根本不同,是吗?一个是使用HttpURLConnection,另一个是HttpClient。最有可能的是,HttpClient 有更严格的检查,这就是你在 Android 上遇到错误的原因。发布完整的堆栈跟踪,并尝试找出错误编码的部分。此外,您不需要使用 Android 中的 VIEWSTATE,这是为了保存表单状态。只需设置服务器端脚本所需的参数(名称等)。
  • 感谢您的帮助,但我刚刚尝试了 setContent("application/x-www-form-urlencoded");它立即起作用了。不过,我感谢您的帮助!
【解决方案2】:

您可以使用 NameValuePair 和 UrlEncodedFormEntity:

List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair(KEY1, VALUE1));
nvps.add(new BasicNameValuePair(KEY2, VALUE2));
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
post.setEntity(p_entity);

【讨论】:

  • 不,我不能。我在其他应用程序中使用过它,它工作得很好,但在这个程序中它给我带来了麻烦。
猜你喜欢
  • 2015-09-30
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多