views:

47

answers:

2

Hi, I need to pass from commons-httpclient-3.0.jar to commons-httpclient-3.1.jar but changing the jar my code doesn't work any more. The problem is that the new library encode automatically the passed uri. Is there a way to avoid this? I must interact with Yahoo API and I mustn't encode the URI otherwise I can't access to the services. Here there is a scratch of my code, comparing the two printing line I observe the difference between the passed URI and the used one.

 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());
        }

And this is the output:

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 failed: HTTP/1.1 401 Forbidden

coppia: oauth_problem signature_invalid

particolarly:

%26&oauth_version --> %2526&oauth_version

and

xoauth_lang_pref="en-us" --> xoauth_lang_pref=%22en-us%22

A: 

Would using setUri("https://api.login.yahoo.com/oauth/v2/get_request_token") followed by setQueryString(insert string here) work? I seem to remember having more control over the query string doing it this way...

Quotidian
Thank you for the answer but I've already tried this solution before and the result is the same.In my project I must use the api commons-httpclient-3.1.jar but I can change the code for calling the yahoo services. Is there another api that can I use for interact whit Yahoo?
AndyPower
A: 

You can avoid encoding by doing this,

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

However, you might get exception on your original URL, which is not properly encoded. You need to encode double quotes. Even better, get rid of it.

ZZ Coder
Thank you very much for the tip! This solved my trouble! I was stupid because I never tought before! :-)
AndyPower