Java help

"Please leave a message at the beep, we will get back to you when your support contract expires."

Moderators: phlip, Larson, Moderators General, Prelates

Java help

Postby Garm » Fri Apr 27, 2012 10:29 pm UTC

Hi all. I'm trying to hack together some java to help with a project at work. My knowledge of programming is what can charitably called limited and I thought maybe someone could help me out. Not sure if this belongs here or in the Coding forum. If this is the wrong place I won't have hurt feelings if it gets moved. :)

I'm trying to send some HTTP commands using URLConnection and HttpURLConnection. I can send a GET request just fine but my POST request isn't working quite as I want it to. Here's my code:

Spoiler:
Code: Select all
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.io.*;
public class AutoTest {
public static void main (String[] args){
   
   BufferedReader rd = null;
   StringBuilder sb = null;
   String line = null;   
   String command = COMMAND
String phoneIP="IPADDRESS";
String[] array={"Value1","Value2","Value3","Value4"};

OutputStream output = null;

int i=0;
try {
while (i<array.length+1) {
String url = phoneIP + command + "?" + array[i];
HttpURLConnection httpConnection = (HttpURLConnection) new URL(url).openConnection();
httpConnection.setRequestMethod("POST");
i++;
rd  = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
sb = new StringBuilder();

while ((line = rd.readLine()) != null)
{
    sb.append(line + '\n');
}

System.out.println(sb.toString());
         }
      } catch (MalformedURLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } finally {
         if (output != null) try {output.close();} catch (IOException logOrIgnore){}
      }
   }
}


What happens right now is that the code sends out a command that looks like "POST /COMMAND?Value1 HTTP/1.1" From this I get a "405 Method Not Allowed." When I send the same command and value using cURL the packet request looks like "POST COMMAND HTTP/1.1 (aplication/x-www-form-urlencoded)" and Value1 is on the interior of the packet. I've searched all over Stackoverflow (they've got a nice tutorial on URLConnection) but can't find a solution to this problem. Any ideas? I can provide more information but need prompting.
Those who make peaceful revolution impossible will make violent revolution inevitable.
- JFK
User avatar
Garm
 
Posts: 2243
Joined: Wed Sep 26, 2007 5:29 pm UTC
Location: Usually at work. Otherwise, Longmont, CO.

Re: Java help

Postby brant » Wed May 02, 2012 10:37 pm UTC

I don't think you're using HTTPUrlConnection correctly to POST. It's hard for me to distinguish the POST data in your code as well. A POST request should look something like this:

Code: Select all
POST /login.jsp HTTP/1.1
Host: www.mysite.com
User-Agent: Mozilla/4.0
Content-Length: 27
Content-Type: application/x-www-form-urlencoded

userid=joe&password=guessme


Notice the POST data is not included in the URL, and instead is included in the payload. You've declared an output stream, but haven't actually used it here as well.

http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139
This example seems to illustrate a proper POST request fairly well, and you should be able to adapt it to your code.
User avatar
brant
 
Posts: 6
Joined: Wed Oct 03, 2007 3:42 am UTC
Location: CT

Re: Java help

Postby Garm » Fri May 04, 2012 7:52 pm UTC

I think that content type is what I'm missing. I may have missed that part of the tutorial I was reading but there's a good example in your link. Thanks. I'll let you know when I get this working.
Those who make peaceful revolution impossible will make violent revolution inevitable.
- JFK
User avatar
Garm
 
Posts: 2243
Joined: Wed Sep 26, 2007 5:29 pm UTC
Location: Usually at work. Otherwise, Longmont, CO.


Return to The Help Desk

Who is online

Users browsing this forum: No registered users and 5 guests