Calling a VB.NET web service with Java
What is the best solution (if any) for calling a vb.net web service with Java / JSP? If anyone could point me to any examples / references I would very much appreciate it. Thanks!
3 Answers 3
For Java to connect to the .NET Web service, it will require that you first generate a WSDL. This can be done by prepending ?WSDL to the .asmx web service (.NET), e.g. http://localhost:8080/myApp/myService.asmx?WSDL
For Java to understand this, you will need a Web Service client: In Java 6, the JAX-WS is the latest specification.
The following client that conforms to the JAX-WS is:
These tools allows you to create a java proxy from the WSDL definition. In java 6 (in JDK_HOME/bin folder) there are tools (now standard) to convert a WSDL definition to java proxy. The wsimport (windows) allows such operation.
Once you have a java proxy, you can basically call all the exposed web service methods created to call your web service.
This totally depends on what kind of web service you are talking about (RESTful vs. SOAP, etc.). In theory, the whole point of having a web service is to provide this kind of interoperability, so it should definitely be possible. However, without more details its hard to point you in the right direction.
Really, this question has nothing to do with VB.net, since the client should not need to be exposed to any of the service implementation details.
EDIT: Since its a SOAP service, you should be able to create a client based off the service’s WSDL. There are a variety of tools to help with this. Here is a tutorial on generating Java code from a WSDL using the WSDL2Java utility.
Java frameworks you should take a look at:
If by «VB.NET web service», you mean an ASP.NET Web Service (.asmx file extension), you should be able to consume that with Java with a Soap client.
Each ASP.NET web service will expose its public contract for that service via a WSDL (Web Service Definition Language) file, which you can access by simply adding ?WSDL after the .asmx of the web service’s address. The key benefit behind web services based on common protocols such as SOAP is that they allow software to communicate across machine and platform boundaries. The WSDL file is the contract that exists between the software applications to make sure they communicate in the same language.
A good client library will read that WSDL file and will handle the plumbing of communicating with the web service via SOAP-based XML messages. If you really wanted to, you could even hand-craft your SOAP XML messages and communicate via a simple HTTP client. However, it would be best to use a library that already exists to handle your SOAP plumbing. This lets your application consume the functionality of the web service while making sure you adhere to the web service contract.
Net web services with java
I cannot use an applet since the web service is on a different machine from the java client.
How do i modify program as to not use an applet.
when Java client try to pass an object to .NET WebService,the WebService always receives
a null object,
We have been facing a similar issue in our shop. I suggest you look up «Flattening out a WSDL».
I don’t know why INTEROP has to be this difficult.
I have used your program SOAPexample.java with new parameters..
import java.net.*;
import java.util.*;
import java.io.*;
String Server = «»;
String WebServicePath = «»;
String SoapAction = «»;
String MethodName = «»;
String XmlNamespace = «»;
private Vector ParamNames = new Vector();
private Vector ParamData = new Vector();
public void AddParameter(String Name, String Data) ParamNames.addElement( (Object) Name);
ParamData.addElement( (Object) Data);
>
public String sendRequest() String retval = «»;
Socket socket = null;
try socket = new Socket(Server, 80);
>
catch (Exception ex1) return («Error1: «+ex1.getMessage());
>
try OutputStream os = socket.getOutputStream();
boolean autoflush = true;
PrintWriter out = new PrintWriter(socket.getOutputStream(), autoflush);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
int length = 295 + (MethodName.length() * 2) + XmlNamespace.length();
for (int t = 0; t < ParamNames.size(); t++) String name = (String) ParamNames.elementAt(t);
String data = (String) ParamData.elementAt(t);
length += name.length();
length += data.length();
>
// send an HTTP request to the web service
out.println(«POST » + WebServicePath + » HTTP/1.1″);
out.println(«Host: ws.mqgroup.com»);
out.println(«Content-Type: text/xml; charset=utf-8»);
out.println(«Content-Length: » + String.valueOf(length));
out.println(«SOAPAction: \»» + SoapAction + «\»»);
out.println(«Connection: Close»);
out.println();
// Read the response from the server . times out if the response takes
// more than 3 seconds
String inputLine;
StringBuffer sb = new StringBuffer(1000);
int wait_seconds = 3;
boolean timeout = false;
long m = System.currentTimeMillis();
while ( (inputLine = in.readLine()) != null && !timeout) sb.append(inputLine + «\n»);
if ( (System.currentTimeMillis() — m) > (1000 * wait_seconds)) timeout = true;
>
in.close();
// The StringBuffer sb now contains the complete result from the
// webservice in XML format. You can parse this XML if you want to
// get more complicated results than a single value.
if (!timeout) String returnparam = MethodName + «Result»;
int start = sb.toString().indexOf(«») +
returnparam.length() + 2;
int end = sb.toString().indexOf(«">«);
//Extract a singe return parameter
retval = sb.toString().substring(start, end);
>
else retval=»Error: response timed out.»;
>
socket.close();
>
catch (Exception ex) return («Error: cannot communicate.»);
>
public static void main(String args[])
SOAPexample s = new SOAPexample();
String response = «Nothing»;
s.Server = «216.174.170.98»;//getParameter(«server»);
s.MethodName = «AESencrypt»;//getParameter(«method»);
s.XmlNamespace = «http://ws.mqgroup.com/»;//getParameter(«xmlnamespace»);
s.WebServicePath = «/MQCrypt/AESCypher.asmx»;//getParameter(«webservicepath»);
s.SoapAction = «http://ws.mqgroup.com/AESencrypt»;//s.XmlNamespace+s.MethodName;
s.AddParameter(«secret», «OH0070»);
s.AddParameter(«password», «1H#*r4u»);
response = s.sendRequest();
But i m not able to get the result.. It is printing.
response >>>>>>>> Error: cannot communicate
Please help me where is the problem.
The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.
POST /MQCrypt/AESCypher.asmx HTTP/1.1
Host: ws.mqgroup.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: «http://ws.mqgroup.com/AESencrypt»
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
I have this code
/*try < // Call Web Service Operation
WebService service = new WebService();
WebServiceSoap port = service.getWebServiceSoap();
String csr = FileUtil.lecturaPEM(dGeneral.getFichCSR());
String endpoint=»https://ipServidor/CA_WS/CAUtils.asmx»;
RetSignCSR result = port.signCSR(usuario.getActivationCode(), csr, usuario.getIdUsuario());
if (result.getCode()!=0)
System.out.println(«Error en el acceso al WS.\n»);
System.out.println(«El código de error es: «+result.getCode());
System.out.println(«\n Esto significa:»+result.getMessage());
>
else < //Process the response
> catch (Exception ex) Logger.getLogger(ApplicationManager.class.getName()).log(Level.SEVERE, null, ex);
>*/
What part of the SOAP you are constructing is included in the length (what part is considered content and where does it begin?)
I am using .net 3.5 version
using C# with asp.net
xml file is generating . before sending to webservice.
but it is generating exception Exception was Unhadled by usercode
but I am not able to rectify that problem.
plz.. give me suggestion.. early.
An applet can’t make a Socket to a Server other than the one where the class-files are. That might be your problem. But you could give us more input for example the thrown exception itself.
The content-length of the HTTP-Request is wrong when you use another number of parameters or if you name them differently. Just make sure the «length» integer contains the right number! Hobbs length-calculating does only work for his example.
-each newline created by println has a character-count of 2 («\n»)!
-make sure you count each methodname-tag and parameter-tag twice (open-tag and close-tag)
It’s the most simple solution to use a LinkedList and write the http-body in it (body starts with POST) and then count the characters of the LinkedList and add 2 for each line. Then you can write the LinkedList into the outputstream.
I hope we help most of you
Hi Alex, thank you for your good explanation.
May I get your own SOAPRequest class (library) at least ?
I am also wondering why i cannot download Hobbs SoapRequestBuilder source code.
Would you like send me the file to my own email ferry_new2004@yahoo.com ?
We are still working on our class, because we want to optimize it for our purpose. Since we have more important stuff to do, it will take some weeks. We may also write an article on codeproject, if we have enough time.
thanks.
so this can done by Apache axis toolkit or any other.
but this is only available when i run the program,and my code in php can’t find host,
how could i use the .asmx?
On doing a bit more of RnD, I came accross a situation when if I add a Web method to existing service with 3 parameters, the method is not accessible and the raised exception is Error: cannot communicate. String index out of range: -16. When there is 1 parameter or no parameter, then the program stalls at
while ( (inputLine = in.readLine()) != null && !timeout) sb.append(inputLine + "\n");
if ( (System.currentTimeMillis() - m) > (1000 * wait_seconds)) timeout = true;
>
What seems to be the issue. This is important.
int length = 295 + (MethodName.length() * 2) + XmlNamespace.length();
int length = 295 + (MethodName.length() * ParamNames.size()) + XmlNamespace.length();
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
Calling a VB.NET web service with Java
What is the best solution (if any) for calling a vb.net web service with Java / JSP? If anyone could point me to any examples / references I would very much appreciate it. Thanks!
3 Answers 3
For Java to connect to the .NET Web service, it will require that you first generate a WSDL. This can be done by prepending ?WSDL to the .asmx web service (.NET), e.g. http://localhost:8080/myApp/myService.asmx?WSDL
For Java to understand this, you will need a Web Service client: In Java 6, the JAX-WS is the latest specification.
The following client that conforms to the JAX-WS is:
These tools allows you to create a java proxy from the WSDL definition. In java 6 (in JDK_HOME/bin folder) there are tools (now standard) to convert a WSDL definition to java proxy. The wsimport (windows) allows such operation.
Once you have a java proxy, you can basically call all the exposed web service methods created to call your web service.
This totally depends on what kind of web service you are talking about (RESTful vs. SOAP, etc.). In theory, the whole point of having a web service is to provide this kind of interoperability, so it should definitely be possible. However, without more details its hard to point you in the right direction.
Really, this question has nothing to do with VB.net, since the client should not need to be exposed to any of the service implementation details.
EDIT: Since its a SOAP service, you should be able to create a client based off the service’s WSDL. There are a variety of tools to help with this. Here is a tutorial on generating Java code from a WSDL using the WSDL2Java utility.
Java frameworks you should take a look at:
If by «VB.NET web service», you mean an ASP.NET Web Service (.asmx file extension), you should be able to consume that with Java with a Soap client.
Each ASP.NET web service will expose its public contract for that service via a WSDL (Web Service Definition Language) file, which you can access by simply adding ?WSDL after the .asmx of the web service’s address. The key benefit behind web services based on common protocols such as SOAP is that they allow software to communicate across machine and platform boundaries. The WSDL file is the contract that exists between the software applications to make sure they communicate in the same language.
A good client library will read that WSDL file and will handle the plumbing of communicating with the web service via SOAP-based XML messages. If you really wanted to, you could even hand-craft your SOAP XML messages and communicate via a simple HTTP client. However, it would be best to use a library that already exists to handle your SOAP plumbing. This lets your application consume the functionality of the web service while making sure you adhere to the web service contract.