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.*;
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) {}
}
}
}