Запрос Android и HTTPS - PullRequest
       313

Запрос Android и HTTPS

0 голосов
/ 31 января 2012

Скажите, пожалуйста, как отправить запрос через https на Android. Я пытаюсь это Но я получаю 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) {}

1 Ответ

0 голосов
/ 05 декабря 2013

Попробуйте этот код .. это вам поможет

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("test.a@gmail.com", "test");
        return null;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...