HttpsURLConnection 连接问题

发布于 2024-09-25 09:44:21 字数 1281 浏览 0 评论 0原文

我遇到了 HttpsURLConnection 的问题,我似乎无法解决。基本上,我向服务器发送一些信息,如果其中一些数据错误,服务器会向我发送 500 响应代码。但是,它还在响应中发送一条消息,告诉我哪一位数据是错误的。问题是,当我读入消息时,该消息始终为空。我认为这是因为在读取流之前总是抛出 filenotfound 异常。我说得对吗?我也尝试读取错误流,但它总是空的。这是一个片段:

    conn = (HttpsURLConnection) connectURL.openConnection();
    conn.setDoOutput(true);
    conn.setConnectTimeout(30000);
    conn.setReadTimeout(30000);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Length",
         Integer.toString(outString.getBytes().length));
    DataOutputStream wr = new DataOutputStream(conn
      .getOutputStream());
    wr.write(outString.getBytes());
    wr.flush();
    wr.close();
    if(conn.getResponseCode>400{

    String response = getErrorResponse(conn);

    public String getErrorResponse(HttpsURLConnection conn) {
    Log.i(TAG, "in getResponse");
    InputStream is = null;
    try {

     //is = conn.getInputStream();
    is = conn.getErrorStream();
    // scoop up the reply from the server
    int ch;
    StringBuffer sb = new StringBuffer();
    while ((ch = is.read()) != -1) {
     sb.append((char) ch);
    }
    //System.out.println(sb.toString());
    return sb.toString();
    // return conferenceId;
   }
    catch (Exception e){
    e.printStackTrace();
    }
    }

I'm a problem with a HttpsURLConnection that I can't seem to solve. Basically, I'm sending up some info to a server and if some of that data is wrong, the server sends me a 500 response code. However, it also sends a message in the response telling me which bit of data was wrong. The problem is that the message is always empty when I read it in. I think this is because a filenotfound exception always gets thrown before the stream can be read. Am I right? I tried reading the errorstream as well but this is always empty. Here's a snippet:

    conn = (HttpsURLConnection) connectURL.openConnection();
    conn.setDoOutput(true);
    conn.setConnectTimeout(30000);
    conn.setReadTimeout(30000);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Length",
         Integer.toString(outString.getBytes().length));
    DataOutputStream wr = new DataOutputStream(conn
      .getOutputStream());
    wr.write(outString.getBytes());
    wr.flush();
    wr.close();
    if(conn.getResponseCode>400{

    String response = getErrorResponse(conn);

    public String getErrorResponse(HttpsURLConnection conn) {
    Log.i(TAG, "in getResponse");
    InputStream is = null;
    try {

     //is = conn.getInputStream();
    is = conn.getErrorStream();
    // scoop up the reply from the server
    int ch;
    StringBuffer sb = new StringBuffer();
    while ((ch = is.read()) != -1) {
     sb.append((char) ch);
    }
    //System.out.println(sb.toString());
    return sb.toString();
    // return conferenceId;
   }
    catch (Exception e){
    e.printStackTrace();
    }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生生漫 2024-10-02 09:44:21

因此,为了跟进这个问题,我是这样解决的:

public static String getResponse(HttpsURLConnection conn) {
    Log.i(TAG, "in getResponse");
    InputStream is = null;
    try {
        if(conn.getResponseCode()>=400){
            is = conn.getErrorStream();
        }
        else{
            is=conn.getInputStream();
        }
        ...read stream...
}

似乎像这样调用它们会产生一个带有消息的错误流。感谢您的建议!

So just to follow up on this, here is how I solved it:

public static String getResponse(HttpsURLConnection conn) {
    Log.i(TAG, "in getResponse");
    InputStream is = null;
    try {
        if(conn.getResponseCode()>=400){
            is = conn.getErrorStream();
        }
        else{
            is=conn.getInputStream();
        }
        ...read stream...
}

It seems that calling them like this produced an error stream with a message. Thanks for the suggestions!

晨敛清荷 2024-10-02 09:44:21

尝试将 content-type 请求属性设置为 "application/x-www-form-urlencoded"

此链接中提到了相同的内容:
http://developers.sun.com/mobility/midp/ttips/HTTPPost/

Content-Length 和 Content-Type 标头至关重要,因为它们告诉 Web 服务器需要多少字节的数据以及由 MIME 类型标识的数据类型。

在 MIDP 客户端中两种最流行的 MIME 类型是 application/octet-stream(用于发送原始二进制数据)和 application/x-www-form-urlencoded(用于发送名称-值对)

Try setting content-type request property to "application/x-www-form-urlencoded"

The same is mentioned on this link:
http://developers.sun.com/mobility/midp/ttips/HTTPPost/

The Content-Length and Content-Type headers are critical because they tell the web server how many bytes of data to expect, and what kind, identified by a MIME type.

In MIDP clients the two most popular MIME types are application/octet-stream, to send raw binary data, and application/x-www-form-urlencoded, to send name-value pairs

蓝眼睛不忧郁 2024-10-02 09:44:21

你能控制服务器吗?换句话说,您是否编写了在服务器上运行并侦听您尝试访问的端口的进程?

如果您这样做了,那么您还应该能够对其进行调试并查看为什么您的进程返回 404。

如果您没有这样做,则描述您的架构(HTTP 服务器、它调用以响应 HTTP(S) 请求的组件等)我们将从那里获取它。

在最简单的情况下,HTTP 服务器是 Apache 服务器,将控制权交给某些 PHP 脚本,这意味着 Apache 无法将您的请求分配给任何内容。最有可能的是 Web 服务器配置错误。请提供更多详细信息,我们将为您提供帮助。

Are you in control of the server? In other words, did you write the process that runs on the server and listens to the port you're trying to access?

If you did, then you should also be able to debug it and see why your process returns 404.

If you didn't, then describe your architecture (HTTP server, the component it invokes to respond to your HTTP(S) request, etc) and we'll take it from there.

In the very simplest case, of an HTTP server being an Apache server yielding control to some PHP script, it means that Apache couldn't assign your request to anything. Most likely a Web server misconfiguration. Provide some more details and we'll help you out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文