我是一个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();
}
}发布于 2010-10-29 19:28:47
下面是我解决这个问题的方法:
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...
}看起来像这样调用它们会产生一个带有消息的错误流。谢谢你的建议!
发布于 2010-09-28 12:23:20
尝试将内容类型请求属性设置为"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
发布于 2010-10-01 07:08:09
你能控制服务器吗?换句话说,您是否编写了在服务器上运行并侦听您试图访问的端口的进程?
如果您这样做了,那么您还应该能够对其进行调试,并查看为什么您的进程返回404。
如果你没有,那么描述你的架构(HTTP服务器,它调用来响应你的HTTP(S)请求的组件,等等),我们将从那里开始。
在最简单的情况下,HTTP服务器是一个Apache服务器,将控制权交给一些PHP脚本,这意味着Apache不能将您的请求分配给任何东西。很可能是Web服务器配置错误。提供更多详细信息,我们将帮助您解决问题。
https://stackoverflow.com/questions/3804295
复制相似问题