Android Http到Https和SSL crt

时间:2014-05-21 11:44:26

标签: android http https ssl-certificate

我正在处理https发布请求。 我成功完成了http post请求,但我不知道如何使用https SSL crt更改它。 如何在项目中添加SSL crt以及如何将http转换为https。 我尝试了很多例子,但我没有得到它。

我的http发布请求代码是.. saVersion是我的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();  

}

}

1 个答案:

答案 0 :(得分:1)

在某些日子之前我遇到了同样的问题,我已经发布到我的博客中。 Refer it。希望它对你有所帮助。