2013-03-12 587 views
1

我试图通过URL登录和我收到的状态码500 httpurlconnevtion问题HttpURLConnection的获取状态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(); 
     } 
    } 
    } 

和我的PARAMS是

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

我得到的logcat

status :500 
getErrorStream() :[email protected] 

谢谢

**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); 

回答

0

状态代码尝试500意味着Internal Server Error。为什么这是抛给你的,只有targetURL背后的服务器知道。

验证您是否正确使用API​​。看一下响应的主体(除了状态码)可能会提供一个提示。

+0

如何检查身体? – Android 2013-03-12 11:56:05

+0

@Android check connection.getErrorStream(); – 2014-10-14 20:44:59

1

500表示Internal Server Error。在你身边可能没有错误,它在服务器上。即使你发送的东西不正确,导致服务器返回500,仍然是服务器问题。

编辑: 欧凯,服务器而应返回类似400 Bad Request代替500 Internal Server Error但我现在发现你的错误:

​​

这里的问题是,你首先从urlParameters的字节使用getBytes其中(引用javadoc):

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

然后你写使用DataOutputStream.writeBytes它(引用的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-Language这这里时并不需要的,你应该设置Content-EncodingUTF-8和,而不是空Accept-Encoding,你应该添加真正的预期MIME类型。我相信这是一个服务器错误,但是如果您正确设置了请求标头,也许它不会出现。

+0

谢谢,java.io.IOException:超过20个字节的内容长度限制是否有任何问题 – Android 2013-03-12 11:11:25

+0

@test是的,这是一个服务器问题。向我们显示服务器代码 – Sulthan 2013-03-12 11:23:19

+0

它与ios完美合作,我在我的问题中添加了服务器代码plz检查 – Android 2013-03-12 11:54:56