我开发了一种显然有效的 HTTP GET 方法。
public class GetMethodEx {
public String getInternetData() throws Exception{
new TrustAllManager();
new TrustAllSSLSocketFactory();
BufferedReader in = null;
String data = null;
try
{
HttpClient client = new DefaultHttpClient();
URI website = new URI("https://server.com:8443/Timesheets/ping");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
response.getStatusLine().getStatusCode();
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) !=null){
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
} finally{
if (in != null){
try{
in.close();
return data;
}catch (Exception e){
e.printStackTrace();
}
}
}
}
这是我的模拟器从 www.google.com 检索响应时的打印屏幕
GOOGLE.COM 运行的屏幕截图
下面的代码是我的检索方法,将其显示在屏幕上。
public class Home extends Activity {
TextView httpStuff;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.httpexample);
httpStuff = (TextView) findViewById(R.id.tvhttp);
new LongOperation().execute("");
}
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
GetMethodEx test = new GetMethodEx();
String returned = null;
try {
returned = test.getInternetData();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return returned;
}
@Override
protected void onPostExecute(String result) {
httpStuff.setText(result);
}
但是,当我用自己的服务器尝试时。
"https://服务器:端口/xwtimesheets/ping"
我有以下屏幕
我的服务器不工作
这是 GetMethodEx 类的编辑版本。 MySSLSocketFactory 允许您连接任何服务器而无需检查其证书。如您所知,这并不安全。我建议您将服务器的证书添加为受设备信任。
顺便说一句,您的服务器证书有效期已过期。即使您将其添加为受信任,您也可能无法连接到您的服务器。
public class GetMethodEx {
public String getInternetData() throws Exception {
BufferedReader in = null;
String data = null;
try {
HttpClient client = new DefaultHttpClient();
client.getConnectionManager().getSchemeRegistry().register(getMockedScheme());
URI website = new URI("https://server.com:8443/XoW");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
response.getStatusLine().getStatusCode();
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) != null) {
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
} finally {
if (in != null) {
try {
in.close();
return data;
} catch (Exception e) {
Log.e("GetMethodEx", e.getMessage());
}
}
}
}
public Scheme getMockedScheme() throws Exception {
MySSLSocketFactory mySSLSocketFactory = new MySSLSocketFactory();
return new Scheme("https", mySSLSocketFactory, 443);
}
class MySSLSocketFactory extends SSLSocketFactory {
javax.net.ssl.SSLSocketFactory socketFactory = null;
public MySSLSocketFactory(KeyStore truststore) throws Exception {
super(truststore);
socketFactory = getSSLSocketFactory();
}
public MySSLSocketFactory() throws Exception {
this(null);
}
@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException,
UnknownHostException {
return socketFactory.createSocket(socket, host, port, autoClose);
}
@Override
public Socket createSocket() throws IOException {
return socketFactory.createSocket();
}
javax.net.ssl.SSLSocketFactory getSSLSocketFactory() throws Exception {
SSLContext sslContext = SSLContext.getInstance("TLS");
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[] { tm }, null);
return sslContext.getSocketFactory();
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)