I connect to two servers (PROD - https, test server - http) on my applications.
on J2ME: I can connect to these two servers without problems. on Android, I canβt connect to the test server. When the connection is http, if I do not use setChunkedStreamingMode
, I cannot get responseCode(StringIndexOutOfBoundsException);
if I use setChunkedStreamingMode
, the response code is 401
. What should I do, where is my mistake?
Here is my Android code, also if you want to see the J2me code, I can add it too.
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 Code:
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)
source share