使用commons-httpclient-3.X.jar的Java问题

时间:2010-06-22 20:07:04

标签: java api yahoo urlencode apache-commons-httpclient

我需要从commons-httpclient-3.0.jar传递到commons-httpclient-3.1.jar 但更改jar我的代码不再起作用了。 问题是新库自动编码传递的uri。 有办法避免这种情况吗? 我必须与Yahoo API交互,我不能编码URI,否则我无法访问服务。 这里有我的代码划痕,比较两个打印行我观察传递的URI和使用的URI之间的区别。

 GetMethod getMethod = new GetMethod();
    try {
        URI uri = new URI(DeliciousApi.generateRequestToken(), false);
        getMethod.setURI(uri);
        System.out.println("Passed URI: " + uri.getURI());
        int statusCode = client.executeMethod(getMethod);
        if (statusCode != HttpStatus.SC_OK) {
            System.out.println("Used URI: " + getMethod.getURI());
            System.err.println("getMethod failed: " + getMethod.getStatusLine());
        }

这是输出:

Passed URI: https://api.login.yahoo.com/oauth/v2/get_request_token?oauth_nonce=ce4630523j788f883f76314ed3965qw9&oauth_timestamp=1277236486&oauth_consumer_key=hd7sHfs5YVFuh3DRTUFgFgF7GcF4RDtsTXStGdRyJJf7WSuShQAShd2JdiwjIibHsU8YFDgshk7hd32xjA6isnNsT7SkbLS8YDHy&oauth_signature_method=plaintext&oauth_signature=53h8x475a66v238j7f43456lhhgg8s7457fwkkdd%26&oauth_version=1.0&xoauth_lang_pref="en-us"&oauth_callback=oob
Used URI:   https://api.login.yahoo.com/oauth/v2/get_request_token?oauth_nonce=ce4630523j788f883f76314ed3965qw9&oauth_timestamp=1277236486&oauth_consumer_key=hd7sHfs5YVFuh3DRTUFgFgF7GcF4RDtsTXStGdRyJJf7WSuShQAShd2JdiwjIibHsU8YFDgshk7hd32xjA6isnNsT7SkbLS8YDHy&oauth_signature_method=plaintext&oauth_signature=53h8x475a66v238j7f43456lhhgg8s7457fwkkdd%2526&oauth_version=1.0&xoauth_lang_pref=%22en-us%22&oauth_callback=oob

getMethod失败:HTTP / 1.1 401 Forbidden

coppia:oauth_problem signature_invalid

particolarly:

%26& oauth_version - > %2526&安培; oauth_version

xoauth_lang_pref =“en-us” - > xoauth_lang_pref =%22en-我们%22

2 个答案:

答案 0 :(得分:0)

使用setUri(“https://api.login.yahoo.com/oauth/v2/get_request_token”)后跟setQueryString(在此处插入字符串)有效吗?我似乎记得有更多的控制查询字符串这样做...

答案 1 :(得分:0)

您可以通过执行此操作来避免编码,

      URI uri = new URI(DeliciousApi.generateRequestToken(), true);

但是,您的原始网址可能会出现例外情况,但未正确编码。你需要编码双引号。更好的是,摆脱它。