Android的 - HTTP GET请求 [英] Android - HTTP GET Request

查看:141
本文介绍了Android的 - HTTP GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个HTTP GET方法,明确工作。

I have developed a HTTP GET Method that clearly works.

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

Here is a print screen of my emulator when retrieving a response from www.google.com

的GOOGLE.COM工作屏幕截图

下面code是我的检索方法在屏幕上显示出来。

The following code is my retrieval method to display it on screen.

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);       
  }

然而,当我尝试用我自己的服务器。

However, when I try it with my own server.

https://开头的服务器:端口/ xwtimesheets /平

"https://server:port/xwtimesheets/ping"

我有以下的屏幕

我的服务器,不工作

推荐答案

您GetMethodEx类下面是经过编辑的版本。 MySSLSocketFactory允许您连接任何服务器,而不检查其证书。正如你所知道的,这是不是安全的。我建议你​​添加你的服务器的证书信任您的设备。

Here is edited version of your GetMethodEx class. MySSLSocketFactory allows you to connect any server without checking their certificate. As you know it, this is not safe. I recommend you to add your servers' certificate as trusted to your device.

顺便说一句您的服务器证书的有效期已过期。即使你将其添加为受信任的,你可能无法连接到服务器。

By the way your servers certificate validity date is expired. Even if you add it as trusted, you may not connect to your server.

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();
    }
}
}

这篇关于Android的 - HTTP GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆