通过SSL使用REST获取json

时间:2013-03-04 14:49:10

标签: java android curl

要使用php连接服务器,我只需使用这样的cUrl

    curl_setopt_array($ch, array(
        CURLOPT_SSL_VERIFYPEER  => false,
        CURLOPT_SSL_VERIFYHOST  => false,
        CURLOPT_RETURNTRANSFER  => true,
        CURLOPT_TIMEOUT         => 320,
        CURLOPT_CONNECTTIMEOUT  => 320,
        CURLOPT_HTTPHEADER      => array('Content-Type: application/json', "Authorization: aaa:bbb", 'X_FORWARDED_FOR: ' . $_SERVER["REMOTE_ADDR"])
));

但我必须在Android中这样做。目前我正在使用HttpsURLConnection,但我得到了身份验证错误400.我做错了什么?这是一段代码:

conn = (HttpsURLConnection) url.openConnection();

        conn.setRequestMethod("GET");

        conn.addRequestProperty("Content-Type: ", "application/json");
        conn.addRequestProperty("Authorization: ", "aaa:bbb");
        conn.addRequestProperty("X_FORWARDED_FOR: ", getLocalIpAddress().toString());

1 个答案:

答案 0 :(得分:0)

我设法使用HttpGet()像这样连接

    HttpClient conn = new DefaultHttpClient();
    HttpGet getter = new HttpGet(link + 
            appendix);
    Log.d("Request", link+appendix);
    getter.addHeader("Content-Type ", "application/json");
    getter.addHeader("Authorization ", "xxx:yyy");
    getter.addHeader("X_FORWARDED_FOR ", "0.0.0.0");