1

我正在尝试通过 url 登录,并且在 httpurlconnevtion 中获得状态代码 500

public static String excutePost(String targetURL, String urlParameters)
  {
    URL url;
    HttpURLConnection connection = null;  
    try {
      //Create connection
      url = new URL(targetURL);
      connection = (HttpURLConnection)url.openConnection();
      connection.setRequestMethod("POST");
      connection.setRequestProperty("Content-Type", 
           "application/x-www-form-urlencoded");
      connection.setRequestProperty("Connection", "Keep-Alive");    
      connection.setRequestProperty("Content-Length", "" + 
               Integer.toString(urlParameters.getBytes().length));
      connection.setRequestProperty("Content-Language", "en-US");  
      connection.setRequestProperty("Accept-Encoding", ""); 
      connection.setUseCaches (false);
      connection.setDoInput(true);
      connection.setDoOutput(true);

      //Send request
      DataOutputStream wr = new DataOutputStream (
                  connection.getOutputStream ());
      wr.writeBytes (urlParameters);
      wr.flush ();
      wr.close ();

      System.out.println("status :"+connection.getResponseCode());
      System.out.println("getErrorStream() :"+connection.getErrorStream());
      //Get Response    
      InputStream is = connection.getInputStream();
      BufferedReader rd = new BufferedReader(new InputStreamReader(is));
      String line;
      StringBuffer response = new StringBuffer(); 
      while((line = rd.readLine()) != null) {
        response.append(line);
        response.append('\r');
      }
      rd.close();
      return response.toString();

    } catch (Exception e) {

      e.printStackTrace();
      return null;

    } finally {

      if(connection != null) {
        connection.disconnect(); 
      }
    }
  }

我的参数是

String urlParameters =
                        "pwd1=" + URLEncoder.encode("DEMO123", "UTF-8") +
                        "&badge=" + URLEncoder.encode("1233", "UTF-8");

我得到了 logcat

status :500
getErrorStream() :libcore.net.http.FixedLengthInputStream@417bc5c0

谢谢你

**EDITED 2**

我也尝试过

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

        // Add badge
        dos.writeBytes(LINE_START + BOUNDRY + LINE_END);
        dos.writeBytes("Content-Disposition: form-data; name='badge';");
        dos.writeBytes(LINE_END + LINE_END);
        dos.writeBytes("1233");
        dos.writeBytes(LINE_END);

        // Add password
        dos.writeBytes(LINE_START + BOUNDRY + LINE_END);
        dos.writeBytes("Content-Disposition: form-data; name='pwd1';");
        dos.writeBytes(LINE_END + LINE_END);
        dos.writeBytes("DEMO123");
        dos.writeBytes(LINE_END);
4

2 回答 2

1

500表示一个Internal Server Error。您这边可能没有错误,它在服务器上。即使您发送错误的内容并导致服务器返回 500,它仍然是服务器问题。

编辑: 好的,服务器应该返回类似的东西,400 Bad Request而不是500 Internal Server Error但我现在发现你的错误:

connection.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length));

...

//Send request
DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
wr.writeBytes (urlParameters);

这里的问题是您首先urlParameters使用getByteswhich 获取字节(引用 javadoc):

使用平台的默认字符集将此字符串编码为字节序列

然后你使用DataOutputStream.writeByteswhich 编写字节(引用 javadoc):

字符串中的每个字符通过丢弃其高八位按顺序写出。

总之,您Content-Length的数据不匹配。所以服务器返回给你

java.io.IOException:超出 20 字节的内容长度限制

解决方案:

//consider urlParameters.getBytes("UTF-8") method instead of using default encoding
byte[] bodyData = urlParameters.getBytes(); 
connection.setRequestProperty("Content-Length", Integer.toString(bodyData.length));

...

//Send request
InputStream out = connection.getOutputStream();
out.write(bodyData);

编辑2: 编辑1肯定是有效的,但是,再次查看问题,我相信错误肯定是由服务器引起的。我认为服务器返回了一个错误的Content-Length标头,当在 Android 上读取数据时,系统意识到来自服务器的数据比它应该的多,Content-Length并引发异常,还将状态代码替换为 500,因为它真的是服务器错误。

编辑3:

connection.setRequestProperty("Content-Language", "en-US");  
connection.setRequestProperty("Accept-Encoding", ""); 

而不是设置Content-Languagewhich is not necessary ,您应该设置Content-EncodingUTF-8and 而不是 empty Accept-Encoding,您应该添加真正预期的 MIME 类型。我相信这是一个服务器错误,但如果您正确设置请求标头,它可能不会出现。

于 2013-03-12T10:46:06.237 回答
0

状态码的500意思Internal Server Error。为什么要扔给你,只有后面的服务器targetURL知道。

验证您是否正确使用了 API。查看响应的正文(除了状态代码)可能会提供提示。

于 2013-03-12T10:44:39.900 回答