Search code examples
javapostmethodsbluejpastebin

Java code: Won't POST correctly


UPDATE: the POST request goes through just fine; the only problem is that when the input code (see below) runs, it posts the output of the input code rather than the input code itself even when the input code is commented out.

Input Code (zigzby): http://pastebin.com/5mZHpX7g was an earlier version of this program that I am using as the input.

Currently the output is "ha" on pastebin.com

Full Code: http://pastebin.com/qGiFUnyK

Specifically:

String zigzby = classEditor.getText(beginningLocation, endingLocation);
String encodedString = URLEncoder.encode(zigzby, "UTF-8");
encodedString = encodedString.replaceAll("%0A", "\r\n");
Scanner input = new Scanner(System.in);
System.out.println("Enter your pastebin code:");
String urlParameters = "api_option=paste&api_paste_code="+zigzby+"&api_dev_key=REVOKED&api_paste_expire_date=1M";
URL myURL = new URL("http://pastebin.com/api/api_post.php");
HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
myURLConnection.setDoOutput(true);
myURLConnection.setRequestMethod("POST");
myURLConnection.connect();

OutputStreamWriter writer = new OutputStreamWriter(myURLConnection.getOutputStream());

writer.write(urlParameters);
writer.flush();

Solution

  • Figured it out! When the input code was being fed in, I forgot that it contained POST parameters in it. These were included in the POST request, so that they were used rather than the original POST parameters.