Android的HTTP到HTTPS和SSL CRT [英] Android Http to Https and SSL crt

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

问题描述

我在工作的HTTPS POST请求。
我做了成功的HTTP POST请求,但我不知道如何使用HTTPS的SSL CRT改变它。
我怎样才能在项目,以及如何为http转换到https添加SSL CRT。
我试过很多例子,但我没有得到它。

I am working on https post request. I did successfully http post request but i don't know how to change it with https SSL crt. How can I add SSL crt in project and how to convert http to https. I tried many examples but i didn't get it.

我的HTTP POST请求code是.. saVersion是我的lib

My http post request code is.. saVersion is my lib

 public class ServerCommunication implements Runnable, IServerCommunication {
 private static final String TAG = ServerCommunication.class.getSimpleName();

 private  String url;
 private  String userAgent;
 private  byte[] data;

 static
 {
        System.loadLibrary("saNative");
 }

private static void receiveBytestream(byte[] stream)
{
    saVersion.getInstance().onSecurePacketReceived(stream);
}

/**
 * Functions as a container to create other (meaningfuller) instances only
 */
public ServerCommunication()
{
    Log.d(TAG, "Note this class is deprecated");
}


private ServerCommunication(String _url, String _userAgent, byte[] _data)
{
    url = _url;
    userAgent = _userAgent;
    data = _data;
}

public void run()
{
    DefaultHttpClient httpclient = new DefaultHttpClient();


    if(url.equals(""))
    {
        Log.e(TAG, "URL is an empty string... aborting sending procedure");
        return;
    }

    // make URL     
    HttpPost httpost = new HttpPost(url);

    StringEntity se;
    try {
        se = new StringEntity(new String(data) + "\r\n");


        httpost.setEntity(se);
        httpost.setHeader("Accept", "application/json");
        httpost.setHeader("Content-Type", "application/json");


        // Get User Agent String
        httpost.setHeader("User-Agent", userAgent); // set string

    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    try {

        HttpResponse response = httpclient.execute(httpost);


        InputStreamReader sr = new InputStreamReader(response.getEntity().getContent());    
        byte[] respContent = IOUtils.toByteArray(sr);           
        receiveBytestream(respContent); 
    } 
    catch (ClientProtocolException e) 
    {
        Log.e(TAG, "AS-Connection error: Probably Internet-Permission is not set in your manifest?");
        e.printStackTrace();

    } 
    catch (IOException e) 
    {
        Log.e(TAG, "AS-Connection error: Probably Internet-Permission is not set in your manifest?");
        e.printStackTrace();

    }
    finally
    {

    }
}    


@Override
public void sendSecurePacket(String _url, byte[] _data, String userAgent) {
    ServerCommunication sc = new ServerCommunication(_url, userAgent, _data);               
    Thread t = new Thread(sc);      
    t.start();  

}

}

推荐答案

我前一些日子面临着同样的问题,我已经发布到我的博客。 <一href=\"http://androiddevelopmentworld.blogspot.in/2013/10/how-to-call-https-webservices-in-android.html\"相对=nofollow>参阅它。希望它可以帮助你对一样的。

I faced this same issue before some days, I have published into my blog. Refer it. Hope it helps you regarding same.

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

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