我正在尝试在 appengine 上使用 Twilio 的 java 客户端库。它似乎没有按原样运行,需要对客户端库进行一些更改。我进行了以下更改,但不起作用:
public TwilioRestClient(String accountSid, String authToken, String endpoint) {
validateAccountSid(accountSid);
validateAuthToken(authToken);
this.accountSid = accountSid;
this.authToken = authToken;
if ((endpoint != null) && (!endpoint.equals(""))) {
this.endpoint = endpoint;
}
/* origial code
* ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager();
* connMgr.setDefaultMaxPerRoute(10);
* this.httpclient = new DefaultHttpClient(connMgr);
*/
//my changes start HERE
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
BasicHttpParams params = new BasicHttpParams();
ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager(params,schemeRegistry);
//my changes END HERE
this.httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter("http.protocol.version",
HttpVersion.HTTP_1_1);
httpclient.getParams().setParameter("http.socket.timeout",
new Integer(CONNECTION_TIMEOUT));
httpclient.getParams().setParameter("http.connection.timeout",
new Integer(CONNECTION_TIMEOUT));
httpclient.getParams().setParameter("http.protocol.content-charset",
"UTF-8");
this.authAccount = new Account(this);
this.authAccount.setSid(this.accountSid);
this.authAccount.setAuthToken(this.authToken);
}
这使它可以编译和运行,但是我得到了这个异常: 原因:org.apache.http.HttpException:Scheme 'https' 未注册。提出请求时。该请求位于此(未更改的)代码中:
public TwilioRestResponse request(String path, String method,
Map<String, String> vars) throws TwilioRestException {
HttpUriRequest request = setupRequest(path, method, vars);
HttpResponse response;
try {
response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
Header[] contentTypeHeaders = response.getHeaders("Content-Type");
String responseBody = "";
if (entity != null) {
responseBody = EntityUtils.toString(entity);
}
StatusLine status = response.getStatusLine();
int statusCode = status.getStatusCode();
TwilioRestResponse restResponse = new TwilioRestResponse(request
.getURI().toString(), responseBody, statusCode);
// For now we only set the first content type seen
for (Header h : contentTypeHeaders) {
restResponse.setContentType(h.getValue());
break;
}
return restResponse;
} catch (ClientProtocolException e1) {
throw new RuntimeException(e1);
} catch (IOException e1) {
throw new RuntimeException(e1);
}
}
有没有人使用过带有twilio的java appengine?我知道这只是一个使用基本身份验证的带有 XML 或 JSON 的 RESTful 请求。我希望使用客户端库而不是花时间自己编写请求编码......有什么想法吗?