Skip to content

Commit

Permalink
Allow GET or POST with default HttpUrlConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
rummble committed Jul 29, 2010
1 parent a11e62c commit c77f8e7
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions src/com/rummble/api/examples/signpost/SignpostExample.java
Expand Up @@ -108,7 +108,7 @@ private static String getQueryString(HttpParameters params)
return sb.toString(); 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); OAuthConsumer consumer = new DefaultOAuthConsumer(consumerKey,consumerSecret);
// NOTE // NOTE
Expand All @@ -130,27 +130,40 @@ private static void defaultPostCall(HttpParameters params,boolean addAccessToken


System.setProperty( "debug", "true" ); 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(); HttpURLConnection request = (HttpURLConnection) methodURL.openConnection();
// Make it a POST if (doPost)
request.setDoOutput(true); {
request.setRequestMethod("POST"); // Make it a POST
request.setDoOutput(true);
request.setRequestMethod("POST");
}


// sign the request // sign the request
consumer.sign(request); consumer.sign(request);


// send the request // send the request
request.connect(); request.connect();


// Add our additional parameters specific to this API call if (doPost)
// Here the example just adds the method {
String parameters = getQueryString(params); // Add our additional parameters specific to this API call
System.out.println("params = [" + parameters + "]"); // Here the example just adds the method
OutputStreamWriter writer = new OutputStreamWriter(request.getOutputStream()); OutputStreamWriter writer = new OutputStreamWriter(request.getOutputStream());
//write parameters //write parameters
writer.write(parameters); writer.write(parameters);
writer.flush(); writer.flush();
}


// debug // debug
System.out.println(request.getResponseCode()); System.out.println(request.getResponseCode());
Expand Down Expand Up @@ -261,13 +274,13 @@ public static void main(String[] args)
// //
// Uncomment to do a call using default java HttpUrlConnection with signpost // 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 // Uncomment to get Apache Commons method call with signpost
// //
apacheMethodCall(params,true); //apacheMethodCall(params,true);






Expand Down

0 comments on commit c77f8e7

Please sign in to comment.