【问题标题】:Twilio client library on AppengineAppengine 上的 Twilio 客户端库
【发布时间】:2012-05-09 07:21:48
【问题描述】:

我正在尝试在 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 中使用过 appengine for java?我知道这只是一个使用基本身份验证的带有 XML 或 JSON 的 RESTful 请求。我希望使用客户端库而不是花时间自己编写请求编码......有什么想法吗?

【问题讨论】:

    标签: java google-app-engine https twilio


    【解决方案1】:

    最新的 Twilio Java 客户端库依赖于无法与 appengine (Apache HttpClient) 配合使用的外部库。 Twilio 的原始 java 客户端代码只使用了 java.net.HttpURLConnection,没有外部依赖。

    我已将这个较旧的 Twilio java 客户端代码添加到 twilio4j(修复了几个错误)。我在生产中使用它。

    TODO:应为 appengine 友好的官方 twilio-java 项目提供补丁。

    【讨论】:

      【解决方案2】:

      基本问题是 Twilio-Java 使用 Apache HttpClient 连接到 Twilio 的服务器,并且开箱即用,它与 Google AppEngine 不兼容。

      可以使用Apache HttpClient on Google AppEngine。您必须 get the source code 并根据这些说明修改 Twilio-Java 客户端库。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多