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

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
Expand All @@ -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());
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
//
//defaultPostCall(params,true);
defaultPostCall(params,true,false);


//
// 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.