Java Beginners

Moderators: zibadian
Number of threads: 1233
Number of posts: 2675

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
I receive SocketException: Connection reset on HTTP POST client Posted by pengoz on 15 May 2012 at 2:45 AM
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();
}
}




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.