HTTP POST Client to REST - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

HTTP POST Client to REST

pengozpengoz Posts: 3Member
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.