HELLO
here's a description of the service deployed in ASP.NET . I need a java Client to access this service. After writing a java client i have not been able to get the desired result. im getting an exception as follows
____________________________________________________________________________
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL:
http://127.0.0.1/test/HelloWorld.asmx/add at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLCon
nection.java:697)
at Client.main(Client.java:45)____________________________________________________________________________
The listing of the add method(ASP.NET) is as follows
SOAP The following is a sample SOAP request and response. The placeholders shown need to be replaced with actual values.
POST /test/HelloWorld.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/test/HelloWorld/add"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<add xmlns="http://tempuri.org/test/HelloWorld">
<a>int</a>
<b>int</b>
</add>
</soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<addResponse xmlns="http://tempuri.org/test/HelloWorld">
<addResult>int</addResult>
</addResponse>
</soap:Body>
</soap:Envelope>
My Java File--> Client.java
import java.io.*;
import java.net.*;
public class Client {
public static void main(
String[] args) throws Exception {
String SOAPUrl = "http://127.0.0.1/test/HelloWorld.asmx";
String xmlFile2Send = "C:\\Madhup Temp\\add.xml";
String SOAPAction = "http://tempuri.org/test/HelloWorld/add";
// Create the connection where we're going to send the file.
URL url = new URL(SOAPUrl);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;
// Open the input file. After we copy it to a byte array, we can see
// how big it is so that we can set the HTTP Cotent-Length
// property.
FileInputStream fin = new FileInputStream(xmlFile2Send);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
// Copy the SOAP file to the open connection.
copy(fin,bout);
fin.close();
byte[] b = bout.toByteArray();
// Set the appropriate HTTP parameters.
httpConn.setRequestProperty( "Content-Length", String.valueOf( b.length ) );
httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
httpConn.setRequestProperty("SOAPAction",SOAPAction);
httpConn.setRequestMethod( "POST" );
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
// Everything's set up; send the XML that was read in to b.
OutputStream out = httpConn.getOutputStream();
out.write( b );
out.close();
// Read the response and write it to standard out.
InputStreamReader isr = new InputStreamReader httpConn.getInputStream());
BufferedReader in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
public static void copy(InputStream in, OutputStream out)
throws IOException {
// do not allow other threads to read from the
// input or write to the output while copying is
// taking place
synchronized (in) {
synchronized (out) {
byte[] buffer = new byte[256];
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead == -1) break;
out.write(buffer, 0, bytesRead);
}
}
}
}
}
Can anyone please help me out as to why i am not able to access the service and the reason ?
By the way the XML file - add.xml
<a>23</a>
<b>54</b>