What is the difference between httpconnection on J2ME and HttpUrlConnection on Android (http error 401)

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) 
+6
source share
2 answers

I solved this problem. I use ip address instead of link. The server was a Sharepoint server, so it tries to connect to a direct sharepoint server, so the server wants authentication :) Do not use ip directly :)

+1
source

If you are using pre-2.3 devices, HTTPUrlConnection has detected problems

http://android-developers.blogspot.com/2011/09/androids-http-clients.html

+2
source

Source: https://habr.com/ru/post/899283/


All Articles