calling Java services from .Net

I would like to know if anyone has any experience calling Java services from .net and any available code samples?

[121 byte] By [MehulShah] at [2008-2-25]
# 1
Follow the steps mentioned in http://www.soapuser.com/server1.html

Use following Client Code

Imports Proxy

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents Button1 As System.Windows.Forms.Button

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.Button1 = New System.Windows.Forms.Button

Me.SuspendLayout()

'

'Button1

'

Me.Button1.Location = New System.Drawing.Point(104, 136)

Me.Button1.Name = "Button1"

Me.Button1.TabIndex = 0

Me.Button1.Text = "Call WS"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 273)

Me.Controls.Add(Me.Button1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Try

Dim pr As New Proxy

Dim str As String = pr.getHi("Smith")

MsgBox(str)

Catch ex As InvalidCastException

End Try

End Sub

End Class


Use Following Proxy Class

Imports System

Imports System.Diagnostics

Imports System.Web.Services

Imports System.Web.Services.Protocols

Imports System.Xml.Serialization

'

'This source code was auto-generated by wsdl, Version=1.0.2914.16.

'

<System.Web.Services.WebServiceBindingAttribute(Name:="TestCommonSrvcSoap", [Namespace]:="urn:AddressFetcher")> _

Public Class Proxy

Inherits System.Web.Services.Protocols.SoapHttpClientProtocol

<System.Diagnostics.DebuggerStepThroughAttribute()> _

Public Sub New()

MyBase.New()

Me.Url = "http://localhost:8080/soap/servlet/rpcrouter"

End Sub

<System.Diagnostics.DebuggerStepThroughAttribute(), _

System.Web.Services.Protocols.SoapRpcMethodAttribute()> _

Public Function getHi(ByVal strname As String) As String

Dim results() As Object = Me.Invoke("getHi", New Object() {strname})

Return CType(results(0), String)

End Function

<System.Diagnostics.DebuggerStepThroughAttribute()> _

Public Function BegingetHi(ByVal strname As String, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult

Return Me.BeginInvoke("getHi", New Object() {strname}, callback, asyncState)

End Function

<System.Diagnostics.DebuggerStepThroughAttribute()> _

Public Function EndgetHi(ByVal asyncResult As System.IAsyncResult) As String

Dim results() As Object = Me.EndInvoke(asyncResult)

Return CType(results(0), String)

End Function

End Class

Use following JAVA Class (Web Service) and deploy on Tomcat Server as mentioned in above URL.

package samples.addressbook;

import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.apache.soap.util.xml.*;

/**
* See \samples\addressbook\readme for info.
*
* @author Matthew J. Duftler (duftler@us.ibm.com)
*/
public class AddressBook
{
public String getHi(String name)
{
return "Hi " + name;
}
}

You will need to a setup as mentioned in above URL . This will require you to download some packages from SUN Website.

Regards,
Kalpesh Vora

KalpeshVora at 2007-9-9 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 2
You can generate proxy code to send requests to a Java web service just as you would any other web service by using the WSDL.exe tool or the Add Web Reference functionality in Visual Studio.

You use WSDL.exe to generate proxy code like this from the SDK command prompt:
wsdl.exe [web service URL]

Daniel Roth

DanielRoth at 2007-9-9 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...

.NET Development

Site Classified