我在我的应用程序上连接到两台服务器(PROD 是 https,测试服务器是 http)。
关于 J2ME:我可以毫无问题地连接到这两个服务器。在 Android 上我无法连接到测试服务器。当连接是http时,如果我不使用setChunkedStreamingMode
,我无法获得responseCode(StringIndexOutOfBoundsException);
如果我使用setChunkedStreamingMode
,响应代码是401
。我该怎么办,我的错在哪里??
这是我的android代码,如果你想看J2me代码,我也可以添加。
URL url = new URL(getUrl());
URLConnection conn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setConnectTimeout(10000);
httpConn.setRequestProperty("User-Agent", util.getDeviceFullModel()
+ " " + util.getSoftwareVersion());
httpConn.setRequestProperty("Accept-Charset", "utf-8");
httpConn.setRequestProperty("Content-Type",
"text/xml; charset=utf-8");
httpConn.setRequestProperty("SOAPAction",
"http://tempuri.org/IAuthenticationServiceForGroup/"+conTypeString);
httpConn.setRequestProperty("Software-Version", AppData.VERSION);
httpConn.setChunkedStreamingMode(getParams().getBytes("UTF8").length);
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.connect();
os = httpConn.getOutputStream();
os.write(getParams().getBytes("UTF8"));
try {
os.close();
} catch (Exception e) {
onError(e);
}
response=httpConn.getResponseCode();
J2ME 代码:
HttpConnection c = (HttpConnection)XConnection.openConnection(XConnection.SERVER + "AuthenticationServiceForGroup.svc");
c.setRequestProperty("User-Agent", XUtil.getDeviceFullModel() + " " + XUtil.getSoftwareVersion());
c.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
c.setRequestProperty("SOAPAction", "http://tempuri.org/IAuthenticationServiceForGroup/"+conType);
c.setRequestProperty("Software-Version", XApp.VERSION);
c.setRequestMethod(HttpConnection.POST);
OutputStream os = null;
os = c.openOutputStream();
os.write(sParams.getBytes());
try {os.close();} catch (Exception e) {}
if (c.getResponseCode() == HttpConnection.HTTP_OK)