Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am writing a Java code using Apache httpclient to call SOAP web service. So far I have written the below piece of code which is working fine.

void post(String strURL, String strSoapAction,  String strXMLFilename) throws Exception{

        File input = new File(strXMLFilename);
        PostMethod post = new PostMethod(strURL);
        RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
        post.setRequestEntity(entity);
        post.setRequestHeader("SOAPAction", strSoapAction);
        HttpClient httpclient = new HttpClient();
        try {
            int result = httpclient.executeMethod(post);
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(post.getResponseBodyAsString());
        } finally {
            post.releaseConnection();
        }
    }

Here, strXMLFilename is SOAP request file taken from SOAP UI(I copied SOAP UI request xml and paste it in some new xml file and then used it). The above tests a service that takes String as an argument.

But now my question is will it be able to test a service that is taking File as an input? I need to send a File to the service.In SOAP UI, I am able to attach a file and test the service but not able to do the same with the above piece of code.

Please help.

share|improve this question

Ok buddy try this one it worked fine for me

  void sendRequest()throws Exception {  
    System.out.println("Start sending " + action + " request");  
    URL url = new URL( serviceURL );  
    HttpURLConnection rc = (HttpURLConnection)url.openConnection();  
    //System.out.println("Connection opened " + rc );  
    rc.setRequestMethod("POST");  
    rc.setDoOutput( true );  
    rc.setDoInput( true );   
    rc.setRequestProperty( "Content-Type", "text/xml; charset=utf-8" );  
    String reqStr = reqT.getRequest();  
    int len = reqStr.length();  
    rc.setRequestProperty( "Content-Length", Integer.toString( len ) );  
    rc.setRequestProperty("SOAPAction", soapActions[actionLookup(action)] );  
    rc.connect();      
    OutputStreamWriter out = new OutputStreamWriter( rc.getOutputStream() );   
    out.write( reqStr, 0, len );  
    out.flush();  
    System.out.println("Request sent, reading response ");  
    InputStreamReader read = new InputStreamReader( rc.getInputStream() );  
    StringBuilder sb = new StringBuilder();     
    int ch = read.read();  
    while( ch != -1 ){  
      sb.append((char)ch);  
      ch = read.read();  
    }  
    response = sb.toString();  
    read.close();  
    rc.disconnect();  
  }     
share|improve this answer
    
what is 'reqT' object on which getRequest is invoked?? – Anand Mar 26 '14 at 5:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.