我遇到了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();
}
}
答案 0 :(得分:3)
所以,只是为了跟进这个,我就是这样解决的:
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...
}
似乎像这样调用它们会产生带有消息的错误流。谢谢你的建议!
答案 1 :(得分:0)
尝试将内容类型请求属性设置为"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
答案 2 :(得分:0)
您是否掌控了服务器?换句话说,您是否编写了在服务器上运行的进程并侦听您尝试访问的端口?
如果你这样做了,那么你也应该能够调试它,看看为什么你的过程返回404。
如果没有,请描述您的体系结构(HTTP服务器,它调用以响应您的HTTP(S)请求的组件等),我们将从那里开始。
在最简单的情况下,HTTP服务器是Apache服务器,对某些PHP脚本产生控制权,这意味着Apache无法将您的请求分配给任何东西。很可能是Web服务器配置错误。提供更多详细信息,我们会帮助您。