Android 和 HTTPS 请求

发布于 2025-01-01 05:26:13 字数 1515 浏览 1 评论 0原文

请告诉我如何在 Android 上通过 https 发送请求。 我试试这个。但我得到了 IOException。

try {
    HostnameVerifier hostnameVerifier = SSLSocketFactory.STRICT_HOSTNAME_VERIFIER;

    DefaultHttpClient client = new DefaultHttpClient();

    SchemeRegistry registry = new SchemeRegistry();
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    socketFactory.setHostnameVerifier((X509HostnameVerifier)hostnameVerifier);
    registry.register(new Scheme("https", socketFactory,443));
    SingleClientConnManager mngr = new SingleClientConnManager(client.getParams(), registry);
    trustEveryone();
    DefaultHttpClient httpClient = new DefaultHttpClient(mngr,client.getParams());

    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
    HttpPost httpPost = new HttpPost(URL); 

    StringEntity se = new StringEntity(obj);
    httpPost.setEntity(se);
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-type", "application/json");

    HttpResponse response = (HttpResponse)httpClient.execute(httpPost);
    StatusLine status = response.getStatusLine();
    if((status.getStatusCode())==200) {
    HttpEntity entity = response.getEntity();
        if(entity!=null) {
            InputStream instream = entity.getContent();
                result = convertStreamToString(instream);
                instream.close();
         } else {
             result=null;
         }
    }
} catch (ClientProtocolException e) {}
  catch (IOException e) {}

Please, tell me how to send a request via https on Android.
I try this. But I get IOExeption.

try {
    HostnameVerifier hostnameVerifier = SSLSocketFactory.STRICT_HOSTNAME_VERIFIER;

    DefaultHttpClient client = new DefaultHttpClient();

    SchemeRegistry registry = new SchemeRegistry();
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    socketFactory.setHostnameVerifier((X509HostnameVerifier)hostnameVerifier);
    registry.register(new Scheme("https", socketFactory,443));
    SingleClientConnManager mngr = new SingleClientConnManager(client.getParams(), registry);
    trustEveryone();
    DefaultHttpClient httpClient = new DefaultHttpClient(mngr,client.getParams());

    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
    HttpPost httpPost = new HttpPost(URL); 

    StringEntity se = new StringEntity(obj);
    httpPost.setEntity(se);
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-type", "application/json");

    HttpResponse response = (HttpResponse)httpClient.execute(httpPost);
    StatusLine status = response.getStatusLine();
    if((status.getStatusCode())==200) {
    HttpEntity entity = response.getEntity();
        if(entity!=null) {
            InputStream instream = entity.getContent();
                result = convertStreamToString(instream);
                instream.close();
         } else {
             result=null;
         }
    }
} catch (ClientProtocolException e) {}
  catch (IOException e) {}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百合的盛世恋 2025-01-08 05:26:13

试试这个代码..这会对你有帮助

public static class getUserLoginAsyncTask extends AsyncTask<Void, Void, Boolean> {

    private void postData(String userName, String eMail) {
        int count = 0;
        int len =5000;

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("url");

        try {
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", userName));
            nameValuePairs.add(new BasicNameValuePair("password", eMail));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            Log.e("log_tag", "--:  "+response);

            InputStream is = response.getEntity().getContent();
            int contentSize = (int) response.getEntity().getContentLength();
            System.out.println("Content size ["+contentSize+"]");
        }
        catch(Exception e)
        {
            Log.e("log_tag", "Error:  "+e.toString());
        }
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        postData("[email protected]", "test");
        return null;
    }
}

Try this code..this will help you

public static class getUserLoginAsyncTask extends AsyncTask<Void, Void, Boolean> {

    private void postData(String userName, String eMail) {
        int count = 0;
        int len =5000;

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("url");

        try {
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", userName));
            nameValuePairs.add(new BasicNameValuePair("password", eMail));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            Log.e("log_tag", "--:  "+response);

            InputStream is = response.getEntity().getContent();
            int contentSize = (int) response.getEntity().getContentLength();
            System.out.println("Content size ["+contentSize+"]");
        }
        catch(Exception e)
        {
            Log.e("log_tag", "Error:  "+e.toString());
        }
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        postData("[email protected]", "test");
        return null;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文