HttpsURLConnection的连接问题
-
25-09-2019 - |
题
我与一个HttpsURLConnection的一个问题,即我似乎无法解决。基本上,我送了一些信息到服务器,如果一些数据是错误的,服务器发送了我一个500响应代码。然而,它也将在响应告诉我哪个位数据的消息是错误的。问题是,消息总是空的,当我在看过。我想这是因为可以读取流之前filenotfound例外总是被抛出。我对吗?我试着读errorstream以及但这始终是空的。这里的一个片段:
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();
}
}
解决方案
所以才跟进这一点,这里是我如何解决它:
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...
}
似乎调用它们像这样产生与消息的错误流。感谢您的建议!
其他提示
尝试内容类型请求属性设置为"application/x-www-form-urlencoded"
此链路上所提到的相同的是: 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
您的服务器的控制权?换句话说,你写在服务器上运行,并侦听的端口,你要访问的过程?
如果你没有,那么你也应该能够调试一下,看看为什么你的过程返回404。
如果你没有,那么你的描述架构(HTTP服务器,它调用来响应你的HTTP(S)请求组件等),我们会从那里。
在非常简单的情况下,一个HTTP服务器是Apache服务器得到控制,一些PHP脚本的,这意味着阿帕奇无法分配您的要求任何东西。最有可能的Web服务器配置错误。提供一些更多的细节,我们会帮助你的。