【问题标题】:Android Http to Https and SSL crtAndroid Http 到 Https 和 SSL crt
【发布时间】:2014-07-10 00:56:57
【问题描述】:

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

我的 http post 请求代码是.. saVersion 是我的库

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

}

}

【问题讨论】:

    标签: android http https ssl-certificate


    【解决方案1】:

    前几天我也遇到过同样的问题,我已将其发布到我的博客中。 Refer it。希望它对您有所帮助。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-10
    • 2017-06-13
    • 2018-05-02
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    相关资源
    最近更新 更多