【问题标题】:code 400 error on post call with android使用 android 调用后出现代码 400 错误
【发布时间】:2014-01-07 09:53:41
【问题描述】:

我正在尝试使用 POST 方法从 Android 应用程序调用 WS 方法。 我做了什么:

String urlServer = GlobalSession.IP + "insert_reportByte";
            Log.d("[Report]", "url address: " + urlServer);
            URL url = new URL(urlServer);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();

            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            connection.setRequestMethod("POST");

            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type",
                    "multipart/form-data");

            DataOutputStream outputStream = new DataOutputStream(
                    connection.getOutputStream());

            outputStream.write(outputByteArray, 0, outputByteArray.length);
            int serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();
            Log.d("ServerCode", "" + serverResponseCode);
            Log.d("serverResponseMessage", "" + serverResponseMessage);
            outputStream.flush();
            outputStream.close();

        } catch (Exception ex) {
            // ex.printStackTrace();
            Log.e("[Report] --- /!\\ Error: ", ex.getMessage());
        }
        return result;

所以我应该向服务发送一个字节数组。但我有一个 400 错误响应。我的问题是:如何获取此类问题的详细信息?因为我在服务器的日志中找不到任何东西,如果我没有详细信息就很难调试......

WS 是这样定义的(在 ASP.NET 中):

[OperationContract]
    [WebInvoke(Method = "POST",
        UriTemplate = "insert_reportByte",
        BodyStyle = WebMessageBodyStyle.Bare)]
    void insert_reportByte(byte[] image);

而被调用的方法如下

public void insert_reportByte(byte[] image)
    {
        MyEntities entities = new MyEntities();
        String base64stringimage = System.Convert.ToBase64String(image,0,image.Length);

        entities.insert_report("admin", "0614141.107346.2001", "test", base64stringimage, "test");

    }

我做错了什么?

谢谢!

【问题讨论】:

  • 有一个名为 Postman 的 chrome 扩展程序可以帮助您。进行相同的调用,但从浏览器中删除 URL / 参数映射上的 WS 错误。
  • 通过使用该方法,我遇到了 413 错误“文件太大”,因为我正在尝试将图像发送到 WS。
  • 我认为您的问题存在于您的 WS 内部,其中允许在请求中传输的最大字节数是有限的。您能否在问题中添加有关您的 WS 的更多信息。
  • 我只是粘贴了WS的代码,谢谢你的帮助!
  • 问题解决了吗?

标签: java android web-services post


【解决方案1】:

您应该更改服务器上applicationHost.config 文件上的uploadReadAheadSizemaxReceivedMessageSize 参数。

这里有一个讨论它的线程

http://forums.iis.net/t/1169257.aspx?Request+Entity+Too+large+413+error+

此链接可能也很有用,因为它解释了为什么应该更改 uploadReadAheadSizemaxReceivedMessageSize 参数以更好地处理文件上传。

http://www.codeproject.com/Articles/521725/413-Request-Entity-Too-Large

更新

尝试使用这个库进行 http 调用。您似乎向服务器发送了错误的请求。我认为这与 ws 期望的参数无关,而是与 android 应用程序发送的 http 标头有关。

https://github.com/loopj/android-async-http

希望对您有所帮助。 :)

【讨论】:

    猜你喜欢
    • 2020-06-05
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多