C# .NET - create a .net Client to an Https java web service

Asked By mostafa hamdy on 28-Jul-11 08:50 AM
Hello all
I have some issue wish you help me in finding solution to it, I have some HTTPS web service created in java, and I want to create a in .Net(C#) client to consume this service  , but really I dont know how to do that , please if there's any body have some sample or useful URL may have  info. help me in doing that please send me
Regards
Motafa
Jitendra Faye replied to mostafa hamdy on 28-Jul-11 08:54 AM

Call the Java Web Service from a .Net Client

In Visual Studio 2008, create a new console application.

Java EE Web Service .Net Client Interoperability

This creates a new solution with a single Console Application project in it.

Right click the project node and choose Add Service Reference.

Java EE Web Service .Net Client Interoperability

In the Add Service Reference Dialog, paste the address of the service metadata endpoint (service address + ?wsdl suffix: http://localhost:9232/Calculator/CalculatorServiceService?wsdl), and click Go. The dialog will get the service metadata and understand the service contract.

Java EE Web Service .Net Client Interoperability

Provide a namespace for the service reference, and click OK.

This will generate the client side proxy that lets you consume the service easily, and the required configuration settings into the configuration file.

Java EE Web Service .Net Client Interoperability

To call the service using the generated client side proxy, open Program.cs and use the following code:

static void Main(string[] args)

{

    CalculatorServiceClient proxy = new CalculatorServiceClient();

    int result = proxy.Add(2, 3);

    Console.WriteLine("Calculator Service returned: " + result.ToString());

}

Run the program and see that the web service is being called and the result is correct.


Follow this link-
http://blogs.msdn.com/b/bursteg/archive/2008/07/19/how-to-call-a-java-ee-web-service-from-a-net-client.aspx
Hope this will help you

TSN ... replied to mostafa hamdy on 28-Jul-11 08:56 AM
hi..

Add web reference

Now we need to mention the WSDL file in our web site. To add the web service reference we must perform the following steps:

  1. Go to Solution Explorer window.
  2. Right click on the project name (in our case its JSimpCalcWebServiceWebSite). A context menu pops up.
  3. Click the Add Web Reference menu. The Add Web Reference dialog opens.

    Create Web Service Client Add Web Reference context menu
    Figure Fourteen: Add Web Reference Context Menu
  4. Copy and paste the WSDL URL from our web brower’s address bar (refer to figure twelve) to Add Web Reference dialog’s address bar and press go button (refer to figure fifteen).

    Create Web Service Client Add Web Reference context menu
    Figure Fifteen: Add Web Reference Dialog
  5. We can see all the methods names of our web service. Give a name to the web reference (in this example its JSimpCalcWebService) and click the Add Reference button.

Step 3. Write code to invoke web service

Using C# we can very easily invoke the web service with few lines of code:

  1. We first design an ASP.net page. The default fie name is Default.aspx (the source is available in zip file).
  2. Induce the web reference in to our code (i.e. Default.aspx.cs). For example:
    using JSimpCalcWebServiceService;
  3. Next, create the object of the web reference.
    JSimpCalcWebServiceService.JSimpCalcWebServiceService proxy = new JSimpCalcWebServiceService.JSimpCalcWebServiceService();
  4. Now, we can access the WebMethod like any other method call. For example:
    proxy.addition(10,20);
    Create Web Service Client C# Code
    Figure Sixteen: Web Methods Invocation Code
The code should look like as follows:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using JSimpCalcWebServiceService;

public partial class _Default : System.Web.UI.Page 
{
    JSimpCalcWebServiceService.JSimpCalcWebServiceService proxy;

    protected void Page_Load(object sender, EventArgs e)
    {
        proxy = new JSimpCalcWebServiceService.JSimpCalcWebServiceService();
    }

    protected void btnAddition_Click(object sender, EventArgs e)
    {
        try
        {
            lblResultAddition.Text = "Result: " + proxy.addition(double.Parse(txtbtnAdditionParameter1.Text), 
								double.Parse(txtbtnAdditionParameter2.Text));
        }
        catch (FormatException)
        {
            lblResultAddition.Text = "Result: Invalide Input";
        }
        UpdatePanelAddition.Update();
    }

    protected void btnSubtraction_Click(object sender, EventArgs e)
    {
        try
        {
            lblResultSubtraction.Text = "Result: " + proxy.subtraction(double.Parse(txtSubtractionParameter1.Text), 
									double.Parse(txtSubtractionParameter2.Text));
        }
        catch(FormatException)
        {
            lblResultSubtraction.Text = "Result: Invalide Input";
        }

        UpdatePanelSubtraction.Update();
    }
    protected void btnMultiplication_Click(object sender, EventArgs e)
    {
        try
        {
            lblResultMultiplication.Text = "Result: " + proxy.multiplication(double.Parse(txtMultiplicationParameter1.Text), 
									double.Parse(txtMultiplicationParameter2.Text));
        }
        catch (FormatException)
        {
            lblResultMultiplication.Text = "Result: Invalide Input";
        }

        UpdatePanelMultiplication.Update();
    }

    protected void btnDivision_Click(object sender, EventArgs e)
    {
        try
        {
            lblResultDivision.Text = "Result: " + proxy.division(double.Parse(txtDivisionParameter1.Text), 
								double.Parse(txtDivisionParameter2.Text));
        }
        catch (FormatException)
        {
            lblResultDivision.Text = "Result: Invalide Input";
        }

        UpdatePanelDivision.Update();
    }

    protected void btnMaximum_Click(object sender, EventArgs e)
    {
        try
        {
            lblResultMaxMin.Text = "Result: " + proxy.maximum(double.Parse(txtMaxMinParameter1.Text), 
							double.Parse(txtMaxMinParameter2.Text));
        }
        catch (FormatException)
        {
            lblResultMaxMin.Text = "Result: Invalide Input";
        }

        UpdatePanelMaxMin.Update();
    }

    protected void btnMinimum_Click(object sender, EventArgs e)
    {
        try
        {
            lblResultMaxMin.Text = "Result: " + proxy.minimum(double.Parse(txtMaxMinParameter1.Text),
								double.Parse(txtMaxMinParameter2.Text));
        }
        catch (FormatException)
        {
            lblResultMaxMin.Text = "Result: Invalide Input";
        }

        UpdatePanelMaxMin.Update();
    }

    protected void btnPower_Click(object sender, EventArgs e)
    {
        try
        {
            lblResultPower.Text = "Result: " + proxy.power(double.Parse(txtPowerParameter1.Text), 
							double.Parse(txtPowerParameter2.Text));
        }
        catch (FormatException)
        {
            lblResultPower.Text = "Result: Invalide Input";
        }

        UpdatePanelPower.Update();
    } 
}
Source Three: Default.aspx.cs Source Code

Step 4. Test web service client application

Now, it's time to test our web service client application by clicking on the Start Debugging toolbar button or by pressing F5 key. The web page should look like the figure below.

Create Web Service Client Web Application Output



for more reference give a look at this link..

http://www.codeproject.com/KB/java/Invoke_Java_WS_In_CS.aspx

Radhika roy replied to mostafa hamdy on 28-Jul-11 10:52 AM
You're simply going to make your .NET service be a client to your Java service:


Right-click your web service project and use "Add Service Reference"
Specify the URL to the WSDL of the Java web service in the "Address" box, then click "Go"
Specify a "Namaespace" through which the Java service will be accessed, for instance "JavaService". Click "Ok".
If everything went well, you should now have a number of classes created for you, under the "YourProjectNamespace.JavaService" namespace.
In particular, you should have one that represents the service itself. Now, a given service may implement more than one service contract (called "port types" in WSDL terms). If the service implements the JavaServiceContract port type, then you should find a class named YourProjectNamespace.JavaService.JavaServiceContractClient. 

Assuming that this contract includes an operation named "JavaOperation", you should call it like this:

int returnValue = 0;
YourProjectNamespace.JavaService.JavaServiceContractClientjavaService =
null;
try {
        javaService = 
            new
YourProjectNamespace.JavaService.JavaServiceContractClient();
        returnValue = javaService.JavaOperation();
}
finally {
    if (javaService != null) {
        ((IDisposable)javaService.)Dispose();
    }
}

 

Hope this will help you.
Sreekumar P replied to mostafa hamdy on 28-Jul-11 11:20 AM

Hi,

There are numerous changes between VS 2005 and VS 2008.  One of the changes has to deal with the way you add a web reference in a project.  Here are the steps to add a web reference to a windows application in  VS 2008.  First step is to right click the project and select Add Service Reference.

 

 

The second step is to select the Advanced button at the bottom of the form:

 

 The third step is to click the Add Web Reference button to open the web reference dialog:

 

You are now able to add your web reference as before:

 

 

 Not as convenient as right clicking the project and adding the web reference but just need to drill down a little further.