Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle the HttpURLConnection calling sequences in the DefaultOAuthProvider.java #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ghost
Copy link

@ghost ghost commented Apr 1, 2011

I just add connection.setDoOutput(true); in the createRequest(String url) method and add connection.getOutputStream();in the sendRequest() method.

I am doing this because I got a problem when i am using signpost doing the OAuth stuff thing. i got a 502 response code when i tried to get the request token. Here is my code:

    OAuthConsumer consumer = new DefaultOAuthConsumer("consumer-key", "consumer-secret");
    OAuthProvider provider = new DefaultOAuthProvider("http://domain.com/oauth/request_token", "http://domain.com/oauth/access_token", "http://domain.com/oauth/authorize");
    System.out.println("Fetching request token...");
    String authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);    

I am using sign-post-1.2.1.1, and i also checked the source code, i think there may have problems with using the HttpURLConnection class. After some googling, i found those two bug reports http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4192018 and http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4146690. I just copy some evaluations of them here in case that you do not want to check it out by yourself. It's all about the HttpURLConnection.

"The calling sequence that you are supposed to follow is:
For request method other than POST or PUT, 1. set request properties; 2. call getInputStream.
For POST and PUT request methods, 1. set request properties; 2. call getOutputStream and write to the outputstream; 3. call getInputStream to send out the request and get the response from server.
You can not call getOutputStream alone and expect the flush method would cause the request to be sent to the server. The only way to send out the request is by calling getInputStream."

It means that Java really cares about the calling sequence when you do the http request. But oauth.signpost.basic.DefaultOAuthProvider.java do not stick to that very well. It create a POST request,
then some other class set the parameters or properties of the request, then it call sendRequest().OK, there is no calling of getOutputStream(). So i implement my own OAuthProvider, just copy the DefaultOAuthProvider source code into it and add connection.setDoOutput(true) in the createRequest() method and call the connecion.getOutputStream() then do nothing, the problems just gone.
So is it a problem?
BTW, i am using jdk-1.6.0-24, Windows XP professinal sp3.
There is also a wired problem, when i switched jdk from jdk-1.6.0-24 to jdk-1.6.0-14, everything works just fine, can anyone help me out there?

Add connection.setDoOutput(true);
Add connection.getOutputStream();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants