Permalink
Browse files

Allow GET or POST with default HttpUrlConnection

  • Loading branch information...
1 parent a11e62c commit c77f8e7b883dbd0d0a4db2ea2e98409de844f38a @rummble committed Jul 29, 2010
Showing with 28 additions and 15 deletions.
  1. +28 −15 src/com/rummble/api/examples/signpost/SignpostExample.java
View
43 src/com/rummble/api/examples/signpost/SignpostExample.java
@@ -108,7 +108,7 @@ private static String getQueryString(HttpParameters params)
return sb.toString();
}
- private static void defaultPostCall(HttpParameters params,boolean addAccessToken)
+ private static void defaultPostCall(HttpParameters params,boolean addAccessToken,boolean doPost)
{
OAuthConsumer consumer = new DefaultOAuthConsumer(consumerKey,consumerSecret);
// NOTE
@@ -130,27 +130,40 @@ private static void defaultPostCall(HttpParameters params,boolean addAccessToken
System.setProperty( "debug", "true" );
- URL methodURL = new URL(HOST);
+ String parameters = getQueryString(params);
+ System.out.println("params = [" + parameters + "]");
+
+ String url = HOST;
+ if (!doPost)
+ {
+ // do GET
+ url = url + "/?" + parameters;
+ }
+ URL methodURL = new URL(url);
HttpURLConnection request = (HttpURLConnection) methodURL.openConnection();
- // Make it a POST
- request.setDoOutput(true);
- request.setRequestMethod("POST");
+ if (doPost)
+ {
+ // Make it a POST
+ request.setDoOutput(true);
+ request.setRequestMethod("POST");
+ }
// sign the request
consumer.sign(request);
// send the request
request.connect();
- // Add our additional parameters specific to this API call
- // Here the example just adds the method
- String parameters = getQueryString(params);
- System.out.println("params = [" + parameters + "]");
- OutputStreamWriter writer = new OutputStreamWriter(request.getOutputStream());
- //write parameters
- writer.write(parameters);
- writer.flush();
+ if (doPost)
+ {
+ // Add our additional parameters specific to this API call
+ // Here the example just adds the method
+ OutputStreamWriter writer = new OutputStreamWriter(request.getOutputStream());
+ //write parameters
+ writer.write(parameters);
+ writer.flush();
+ }
// debug
System.out.println(request.getResponseCode());
@@ -261,13 +274,13 @@ public static void main(String[] args)
//
// Uncomment to do a call using default java HttpUrlConnection with signpost
//
- //defaultPostCall(params,true);
+ defaultPostCall(params,true,false);
//
// Uncomment to get Apache Commons method call with signpost
//
- apacheMethodCall(params,true);
+ //apacheMethodCall(params,true);

0 comments on commit c77f8e7

Please sign in to comment.