# How to invoke a service from webmethod from java using httpClient ,POST

**URL:** <https://techcommunity.softwareag.com/t/how-to-invoke-a-service-from-webmethod-from-java-using-httpclient-post/124603>\
**Category:** Feedback & ideas\
**Tags:** Discussions-on-this-Forum-System, New-User-Welcome-and-Introductions\
**Created:** [January 17, 2007, 4:05pm UTC](https://techcommunity.softwareag.com/t/how-to-invoke-a-service-from-webmethod-from-java-using-httpclient-post/124603 "2007-01-17T16:05:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![preetibhatta](https://avatars.discourse-cdn.com/v4/letter/p/7cd45c/32.png) [@preetibhatta](https://techcommunity.softwareag.com/u/preetibhatta)\
**Post date:** [January 17, 2007, 4:05pm UTC](https://techcommunity.softwareag.com/t/how-to-invoke-a-service-from-webmethod-from-java-using-httpclient-post/124603/1 "2007-01-17T16:05:20Z")

</div>

Hi,  
I want to post one xml file to a webmethod service from java code using httpClient.  
Can anybody help me on this ?  
My piece of code look like this but its gives me error:

Code :  
import org.apache.commons.httpclient.Credentials;  
import org.apache.commons.httpclient.HttpClient;  
import org.apache.commons.httpclient.HttpStatus;  
import org.apache.commons.httpclient.UsernamePasswordCredentials;  
import org.apache.commons.httpclient.auth.AuthScope;  
import org.apache.commons.httpclient.methods.PostMethod;  
import org.apache.commons.httpclient.methods.GetMethod;  
import java.io.BufferedReader;  
import java.io.InputStreamReader;  
import [java.io](http://java.io).\*;  
public class invokexmlFile {  
public static void main(String args) {  
HttpClient client = new HttpClient();  
// client.getParams().setParameter(“http.useragent”, “Test Client”);  
BufferedReader br = null;  
PostMethod method = new PostMethod(URL);  
method.setRequestHeader(“Cache-Control”, “no-cache”);  
method.setRequestHeader(“Accept”,“text/xml”);  
method.setRequestHeader(“Content-Encoding”,“UTF-8”);  
try{  
File f = new File(“C:\SignOnRQ.xml”);  
method.setRequestBody(new FileInputStream(f));  
}catch(Exception e)  
{}  
client.getParams().setAuthenticationPreemptive(true);  
Credentials defaultcreds = new UsernamePasswordCredentials(“userid”, “password”);  
client.getState().setCredentials(new AuthScope(“servername”,port, AuthScope.ANY\_REALM), defaultcreds);  
client.setConnectionTimeout(8000);  
try{  
int returnCode = client.executeMethod(method);  
if(returnCode == HttpStatus.SC\_NOT\_IMPLEMENTED) {  
System.err.println(“The Post method is not implemented by this URI”);  
// still consume the response body  
method.getResponseBodyAsString();  
} else {  
br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));  
String readLine;  
while(((readLine = br.readLine()) != null)) {  
System.err.println(readLine);  
}  
}  
} catch (Exception e) {  
System.err.println(e);  
} finally {  
method.releaseConnection();  
if(br != null) try { br.close(); } catch (Exception fe) {}  
}  
}  
}
