How to invoke a webmethod service from java using HttpClient + post

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) {}
}
}
}

What is the error you are encountering?

You might find this post helpful.

Mark

I didn’t look through your code, but this works for me day in and day out. The flow service must accept a node and the port, service, and possibly username must be allowed on the IS Admin page.

    public void postURL() {
        try {
            Properties properties = readProperties(propertyFileName);
            String hostname = properties.getProperty("host");
            int port = Integer.parseInt(properties.getProperty("portNumber"));
            String inFile = properties.getProperty("requestFileName");
            String outFile = properties.getProperty("responseFileName");
            String path = properties.getProperty("path");
            String username = properties.getProperty("username");
            String password = properties.getProperty("password");
            String type = "text/xml";
            
            String input = readFile(inFile);
            logger(input);
            String urlStr = "http://" + hostname + ":" + port + path;
            logger("URL is " + urlStr);
            
            URL url = new URL(urlStr);
            HttpURLConnection uc = (HttpURLConnection) url.openConnection();
            String authUser = username + ":" + password;
            String encoding = new sun.misc.BASE64Encoder()
                    .encode(authUser.getBytes());
            uc.setRequestMethod("POST");
            uc.setRequestProperty( "Content-Type", type );
            uc.setRequestProperty("Authorization", "Basic " + encoding);
            uc.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(uc.getOutputStream());
            wr.flush();
            OutputStream os = uc.getOutputStream();
            os.write( input.getBytes() );
            String response = readResponse(uc.getInputStream());
            logger("Response Code is " + uc.getResponseCode());
            logger("Response Message is " + uc.getResponseMessage());
            //logger(response);
            //writeFile(outFile, response);
            wr.close();
        } catch (Exception e) {
            handleException(e);
        }
    }

Preeti - it appears your HTTP client is not setting the HTTP Content-type header to “text/xml”