【发布时间】:2011-06-23 00:41:38
【问题描述】:
在我的 Android 学习过程中又遇到了一个小障碍。
这是我的代码:
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
byte[] encodedPassword = (user + ":" + pass).getBytes();
String auth = "Basic " + Base64.encodeToString(encodedPassword, false);
con.setRequestProperty("Authorization", auth);
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type","text/xml");
con.setRequestProperty("Content-Length","" + Integer.toString(urlParameters.getBytes().length));
con.setRequestProperty("Content-Language", "en-US");
con.setRequestProperty("Connection", "close");
con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(true);
con.setAllowUserInteraction(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int statusCode = ((HttpURLConnection) con).getResponseCode();
Log.d(TAG, "Response Code = " + statusCode + " Content-Length = " + con.getContentLength());
我得到一个响应代码 = 200 和内容长度 = 2593,所以我知道我可以访问该文件
DataInputStream re = new DataInputStream(con.getInputStream());
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
XMLmyHandler myHandler = new XMLmyHandler();
xr.setContentHandler(myHandler);
xr.parse(new InputSource(re));
该文件格式正确,我将其复制到本地非安全 http 服务器,它运行良好。
遗憾的是,当我尝试从安全 http 执行相同操作时,它不起作用。
另外,通过我的非安全 http 成功尝试,我使用 HttpClient 来获取流,而不是这种方法。
但是,我尝试将 HttpClient 与安全 http 一起使用的尝试失败了。
我宁愿保留这种方法,如果你知道任何从我的“con”中提取流的方法,它适用于 SAX,请告诉我!!!提前感谢我得到的任何帮助。
【问题讨论】:
-
失败是什么意思?您能否包括您获得的异常或您看到的错误?内容长度是否返回 0?
-
您尝试使用 https 的结果如何?异常、不同的状态码、来自服务器的格式错误的输出?
-
嗨,我没有得到任何异常。它只是没有像文件长度为 0 那样做任何事情
标签: android xml https sax httpurlconnection