之间的HttpConnection在J2ME和Android上的区别HttpURLConnection的是什么(HTTP错误401)的是、区别、错误、HttpConnection

2023-09-05 03:47:19 作者:不离不弃、放TM狗屁

我连接到两台服务器(PROD为https,测试服务器的HTTP)我applicaitons。

在J2ME:我可以连接到这两个服务器没有问题。 在Android上我无法连接到测试服务器。当连接HTTP,如果我不使用 setChunkedStreamingMode ,我不能让响应code(的StringIndexOutOfBoundsException); 如果我用 setChunkedStreamingMode ,响应code是 401 。我该怎么办,哪里是我的错??

下面是我的Andr​​oid code,另外,如果你想看到J2me的code,我可以添加这一点。

 网​​址URL =新的URL(的getURL());
            URLConnection的康恩= url.openConnection();
            HttpURLConnection的httpConn =(HttpURLConnection类),康涅狄格州;
            httpConn.setAllowUserInteraction(假);
            httpConn.setInstanceFollowRedirects(真正的);
            httpConn.setConnectTimeout(10000);
            httpConn.setRequestProperty(用户代理,util.getDeviceFullModel()
                    ++ util.getSoftwareVersion());
            httpConn.setRequestProperty(接收字符集,UTF-8);

            httpConn.setRequestProperty(内容类型,
                    为text / xml;字符集= UTF-8);
            httpConn.setRequestProperty(SOAPAction报,
                    http://tempuri.org/IAuthenticationServiceForGroup/"+conTypeString);
            httpConn.setRequestProperty(软件版本,AppData.VERSION);
            httpConn.setChunkedStreamingMode(getParams()方法的GetBytes(UTF-8)的长度。);
            httpConn.setRequestMethod(POST);
            httpConn.setDoOutput(真正的);
            httpConn.setDoInput(真正的);
            httpConn.connect();

            OS = httpConn.getOutputStream();
            os.write(getParams()方法的GetBytes(UTF-8));

            尝试 {
                os.close();
            }赶上(例外五){
                onError的(E);
            }
            响应= httpConn.getResponse code();
 
第三章 实现Eclipse Android与J2me平台切换 下

J2ME code:

 的HttpConnection C =(HttpConnection的)XConnection.openConnection(XConnection.SERVER +AuthenticationServiceForGroup.svc);

            c.setRequestProperty(用户代理,XUtil.getDeviceFullModel()++ XUtil.getSoftwareVersion());
            c.setRequestProperty(内容类型,为text / xml;字符集= UTF-8);
            c.setRequestProperty(SOAPAction的,http://tempuri.org/IAuthenticationServiceForGroup/"+conType);
            c.setRequestProperty(软件版本,XApp.VERSION);
            c.setRequestMethod(HttpConnection.POST);

            OutputStream的OS = NULL;

            OS = c.openOutputStream();
            os.write(sParams.getBytes());

            尝试{os.close();}赶上(例外五){}

            如果(c.getResponse code()== HttpConnection.HTTP_OK)
 

解决方案

我解决了这个问题。我使用的IP联系地址,而不是链接。服务器SharePoint服务器的话,它会尝试直接连接到SharePoint服务器,因此服务器需要身份验证:)不要直接使用IP:)

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.write(sParams.getBytes());

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

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

解决方案

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:)