Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

HTTP POST Client to REST

I'm still new to java and would love the help of the community. I'm trying to build a HTTP post client, I'm not sure what am I doing wrong because I receive java.net.SocketException: Connection reset.Here is my class,please help.

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;


public class WebServicePost{

public static void main(String[] args) throws Exception{


String request = "http://api.geonames.org/findNearByWeatherXML?";


HttpClient client = new HttpClient();
PostMethod method = new PostMethod(request);

method.setRequestHeader("Content-type","text/xml; charset=UTF-8");
method.addParameter("lat","43");
method.addParameter("lng","-2");
method.addParameter("username","demo");


// Send POST request
int statusCode = client.executeMethod(method);

if(statusCode != HttpStatus.SC_OK){
System.err.println("Method failed: " + method.getStatusLine());
}
InputStream rstream = null;

//Get the response body
rstream = method.getResponseBodyAsStream();

// Process the response from a Web Services
BufferedReader br = new BufferedReader(new InputStreamReader(rstream));
String line ;
while((line = br.readLine()) != null){
System.out.println(line);
}
br.close();
}
}
Sign In or Register to comment.