在我的Android应用程序,我不得不存储在一台服务器读的XML文件。由于这是一个安全的网页[SSL(HTTPS),访问到的位置( https://开头serverAddress /路径/
)在XML文件所在,通常它需要用户名/密码认证
以下是code,我用阅读和获取XML数据流。但它总是转到异常,当它试图执行的Htt presponse HTT presponse = httpClient.execute(HTTPGET);
语句。异常说不受信任的服务器证书
。
也开始加入了互联网访问权限<使用许可权的android:NAME =android.permission.INTERNET对/>
在清单文件
如果将XML文件存储在它不需要任何身份验证的地方,那么它工作正常。我到处搜寻,但没有找到,将做到这一点的例子。如果有人可以通过这个过程指导,我将非常感激。在此先感谢...... !!!!
尝试 { Log.v(国家,开始吧...); HttpClient的HttpClient的=新DefaultHttpClient(); HTTPGET HTTPGET =新HTTPGET(HTTPS://serverAddress/path/MyXMLFile.xml); httpGet.addHeader(BasicScheme.authenticate(新UsernamePasswordCredentials(用户名,密码),HTTP.UTF_8,FALSE)); HTT presponse HTT presponse = httpClient.execute(HTTPGET); InputStream的xmlInputStream = HTT presponse.getEntity()的getContent()。 Toast.makeText(getApplicationContext(),this.convertStreamToString(xmlInputStream),Toast.LENGTH_LONG).show(); Log.v(国家,完成......); } 赶上(例外五) { Log.v(国家,e.getMessage()的toString()); }
解决方案
这是不是 HTTP
的问题是 HTTPS
- 您使用的是安全连接。我也有很多与麻烦。作为一个快速修复你可以得到你的客户使用信任所有证书此irreplaceable帖子。
后来只考虑信任相应的证书。
修改:因为我知道如何挑剔因此用户。是的,它是不好的做法,信任所有证书,但可以在3分钟内完成,并且可以测试你的code的其余部分。仍然没有忘记这是潜在的安全漏洞,你应该切换到仅信任指定的证书。
In my android application I have to a read an XML file that is stored in a server. Since this is a secured web page[SSL(https)], to access to the location(https://serverAddress/path/
) where the XML file resides, normally it requires username/Password Authentication
Following is the Code that I used to read and get the XML Stream. But it always goes to the exception when it trying to execute the HttpResponse httpResponse = httpClient.execute(httpGet);
statement. Exception says Not trusted server certificate
.
Also have added the internet access permission <uses-permission android:name="android.permission.INTERNET" />
in the Manifest file
If the XML file is stored in a place where it doesn't require any authentication, then it works fine. I searched everywhere but didn't find any example that will do this. If someone can guide through this process I would be really grateful. Thanks in advance...!!!!
try
{
Log.v("State","Started...");
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("https://serverAddress/path/MyXMLFile.xml");
httpGet.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials("username", "password"),HTTP.UTF_8, false));
HttpResponse httpResponse = httpClient.execute(httpGet);
InputStream xmlInputStream = httpResponse.getEntity().getContent();
Toast.makeText(getApplicationContext(), this.convertStreamToString(xmlInputStream), Toast.LENGTH_LONG).show();
Log.v("State","Finish...");
}
catch(Exception e)
{
Log.v("State",e.getMessage().toString());
}
解决方案
This is not http
question it is https
- you are using secure connection. I also had a lot of troubles with those. As a quick fix you can get your client to trust all certificates using this irreplaceable post.
Later on consider trusting only the appropriate certificates.
EDIT: because I know how picky SO users are. Yes it is bad practise to trust all certificates, but this can be done in 3 minutes and you can test the rest of your code. Still never forget this is potential security breach and you should switch to trusting only specified certificate.