无法从Android向REST API发出安全HTTPS POST请求

无法从Android向REST API发出安全HTTPS POST请求,android,rest,post,https,last.fm,Android,Rest,Post,Https,Last.fm,我正在尝试向RESTful API(last.fm)发送HTTPS帖子;然而,REST响应似乎表明我没有使用HTTPS连接。我的代码是这样的(为了简洁起见,我只保留了最相关的部分)。我的代码有什么问题 public String getSessionKey(String username, String pass) throws NoSuchAlgorithmException, ClientProtocolException, IOException { Http

我正在尝试向RESTful API(last.fm)发送HTTPS帖子;然而,REST响应似乎表明我没有使用HTTPS连接。我的代码是这样的(为了简洁起见,我只保留了最相关的部分)。我的代码有什么问题

public String getSessionKey(String username, String pass) throws 
         NoSuchAlgorithmException, ClientProtocolException, IOException {

    HttpClient hc = createHttpClient();

    // Construct API signature string 
    String api_sig = "<constructed api sig goes here>"

    // compute md5 hash of API signature string using md5 method    
    String api_sig_md5 = md5(api_sig); 


    HttpPost hp_auth = new HttpPost("http://ws.audioscrobbler.com/2.0/"
                + "?method=auth.getMobileSession" 
                + "&username=" + username 
                + "&password=" + pass 
                + "&api_key=" + getString(R.string.lastfm_key)
                + "&api_sig=" + api_sig_md5);

    HttpResponse resp = hc.execute(hp_auth);

    // code to convert/parse "resp" XML response to retrieve the session key

    return key; // return session key
}

private HttpClient createHttpClient()
{
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(params, true);

    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);

    return new DefaultHttpClient(conMgr, params);
}
公共字符串getSessionKey(字符串用户名、字符串传递)抛出
NoSuchAlgorithmException、ClientProtocolException、IOException{
HttpClient hc=createHttpClient();
//构造API签名字符串
字符串api_sig=“”
//使用md5方法计算API签名字符串的md5哈希
字符串api_sig_md5=md5(api_sig);
HttpPost hp_auth=新的HttpPost(“http://ws.audioscrobbler.com/2.0/"
+“?方法=身份验证getMobileSession”
+“&username=“+username”
+“&password=“+pass”
+“&api_key=“+getString(R.string.lastfm_key)
+“&api_sig=“+api_sig_md5”);
HttpResponse resp=hc.execute(hp_auth);
//转换/解析“resp”XML响应以检索会话密钥的代码
return key;//返回会话密钥
}
私有HttpClient createHttpClient()
{
HttpParams params=新的BasicHttpParams();
HttpProtocolParams.setVersion(params,HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params,HTTP.DEFAULT\u CONTENT\u CHARSET);
HttpProtocolParams.setUseExpectContinue(params,true);
SchemeRegistry schReg=新SchemeRegistry();
寄存器(新方案(“http”,PlainSocketFactory.getSocketFactory(),80));
寄存器(新方案(“https”,SSLSocketFactory.getSocketFactory(),443));
ClientConnectionManager conMgr=new-ThreadSafeClientConnManager(params,schReg);
返回新的DefaultHttpClient(conMgr,params);
}
我解决了我的问题(这是一个很小的问题)。我在URL中使用http而不是https。所以下面这行应该是

HttpPost hp_auth = new HttpPost("https://ws.audioscrobbler.com/2.0/"
                + "?method=auth.getMobileSession" 
                + "&username=" + username 
                + "&password=" + pass 
                + "&api_key=" + getString(R.string.lastfm_key)
                + "&api_sig=" + api_sig_md5);