How to create a java SOAP Client to test a .Net web service



This is how you can test your web service in Java without creating any SOAP Client. Using this you can access Web Services created using any technology, in specific .Net.


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fugoconsulting;

import java.io.InputStreamReader;
import java.net.URL;

/**
 *
 * @author pandi
 */
public class sendRequest {
 
    public String sendRequestToIRDA(String request) {

    String response = "";
    try {
            String soapXml = request;
            URL url = new URL("http://127.0.0.1/Service/TestService.asmx"); //for testing .Net Service
            java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
            conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            conn.setRequestProperty("SOAPAction", "http://test/Service/Test");
            conn.setRequestProperty("Content-Length", "200000");
            conn.setRequestProperty("Host", "127.0.0.1");
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            System.out.println("String "+soapXml);
            java.io.OutputStream os = conn.getOutputStream();
            java.io.OutputStreamWriter osw = new java.io.OutputStreamWriter(os);
            osw.write(soapXml);
            osw.flush();
            osw.close();

            java.io.InputStream is;
            if(conn.getResponseCode() >= 400) {
                is = conn.getErrorStream();
            }
            else {
                is = conn.getInputStream();
            }
            InputStreamReader isr = new InputStreamReader(is);
            java.io.BufferedReader br = new java.io.BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                response = response.concat(line);
            }

        } catch ( Exception e ) {
            e.printStackTrace();
       }
       return response;
    }
}

Comments

Popular posts from this blog

Changing Timezone in Postgresql, Ubuntu

Display Logic and read only logic In Openbravo

My 10 years in Application Development