我正在尝试做一个简单的HttpGet来阅读网页.我有这个在iOS上工作,并通过http在Android上工作,但不是https.
url是一个内部网络IP和自定义端口,所以我可以使用路径来读取这样的http http://ipaddress:port/MyPage.html
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpResponse response;
String responseString = null;
try {
// Try connection
HttpGet get = new HttpGet(params[0].path);
get.addHeader("Authorization",
"Basic "
+ Base64.encodeBytes(new String(params[0].username + ":" + params[0].password)
.getBytes()));
response = httpclient.execute(get);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else {
// Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
Log.e(TAG, "ClientProtocolException");
this.e = e;
} …Run Code Online (Sandbox Code Playgroud) 编辑:"下面的代码工作正常,没有错误,没有例外"
我知道关于这个主题的大量问题,以及谷歌让人想起的许多博客.我已经通读了它们,并设法提出了我要解释的内容.我的疑问在于" 我的方法是否正确?它是否有任何副作用? "另一个问题在我解释我的方法时更好.
我在此Android.Developres教程之后使用此方法.
System.setProperty("jsse.enableSNIExtension", "false");
//Java 7 introduced SNI (enabled by default). The server I use is
// misconfigured I suppose and
// it sends an "Unrecognized Name" warning in the SSL handshake
// which breaks my web service.
// Load CA from an InputStream (CA would be saved in Raw file,
// and loaded as a raw resource)
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream caInput = new BufferedInputStream(new FileInputStream("PATH_TO_CERT.crt"));
Certificate ca;
try {
ca = cf.generateCertificate(caInput); …Run Code Online (Sandbox Code Playgroud) 我正在开发一个企业Android应用程序,因此有必要在我的测试阶段在客户端(android模拟器/测试手机)和服务器之间创建一个安全的连接,即使服务器的证书是自签名的,而正在购买合法的证书由公司(我现在控制的东西).
我需要信任服务器的自签名证书及其证书授权,当然,它本身不受Android操作系统的信任.我正在遵循谷歌关于在这个场景中几乎逐字创建HTTPS环境的建议.
我目前面临的问题是,我无法.crt从Google的示例中访问此行中的文件:
InputStream caInput = new BufferedInputStream(
new FileInputStream("load-der.crt"));
Run Code Online (Sandbox Code Playgroud)
代替上述,我使用:
InputStream caInput = new BufferedInputStream(
getResources().openRawResource(R.raw.mycrtfile));
Run Code Online (Sandbox Code Playgroud)
打开文件所在的InputStream派生自.但是,我得到了一条线. mycrtfile.crt.crt/res/raw/mycrtfile.crtNullPointerException
有没有更好的方式来存储和访问,我需要加载作为的证书文件InputStream或FileInputStream不是存储在内部的原始资源res目录?