IBM webMethods Hybrid Integration

IBM webMethods Hybrid Integration

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
Expand all | Collapse all

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

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

    Posted Wed January 17, 2007 11:12 AM

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


    #Flow-and-Java-services
    #webMethods
    #Integration-Server-and-ESB


  • 2.  RE: How to invoke a webmethod service from java using HttpClient + post

    Posted Wed January 17, 2007 04:55 PM

    What is the error you are encountering?


    #webMethods
    #Integration-Server-and-ESB
    #Flow-and-Java-services


  • 3.  RE: How to invoke a webmethod service from java using HttpClient + post

    Posted Thu January 18, 2007 01:35 AM


  • 4.  RE: How to invoke a webmethod service from java using HttpClient + post

    Posted Wed January 31, 2007 06:18 PM

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

    #webMethods
    #Integration-Server-and-ESB
    #Flow-and-Java-services


  • 5.  RE: How to invoke a webmethod service from java using HttpClient + post

    Posted Tue February 06, 2007 06:55 AM

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


    #webMethods
    #Flow-and-Java-services
    #Integration-Server-and-ESB