我正在尝试对RESTful API进行HTTPS POST(last.fm);但是,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);
}
答案 0 :(得分:0)
我想出了我的问题(这非常简单)。我在URL而不是https中使用http。所以下面的行应该是
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);