开发者

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

开发者 https://www.devze.com 2023-04-12 19:18 出处:网络
I connect to two servers (PROD is https, test server is http) on my applicaitons. on J2ME: I can connect to this two servers without a problem.

I connect to two servers (PROD is https, test server is http) on my applicaitons.

on J2ME: I can connect to this two servers without a problem. on Android I can't connect to test-server. When connection is http, if I dont use setChunkedStreamingMode, I cant get responseCode(StringIndexOutOfBoundsException); if I use setChunkedStreamingMode, response code is 401. What should I do, where is my fault??

Here is my android code, Also if you want to see 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.w开发者_如何学Gorite(sParams.getBytes());

            try {os.close();} catch (Exception e) {}

            if (c.getResponseCode() == HttpConnection.HTTP_OK)


If you're using pre-2.3 devices, HTTPUrlConnection has known issues

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


I solved this problem. I use ip adress instead of link. Server was Sharepoint server so, It tries to connect to directly sharepoint server, so server wants Authentication:) Dont use directly ip:)

0

精彩评论

暂无评论...
验证码 换一张
取 消