Zugriff auf .NET WebService - ohne fremde Klassen

StehtimSchilf

Erfahrenes Mitglied
Hi Forum

neue Woche, neues Problem :(

ich versuche verzweifelt via Java einen .NET-WebService anzusprechen - ohne Zusatz von irgendwelchen Fremd-Klassen. Dies ist eine Bedingung!

Aber irgendwie hat sich irgendwo einen Fehler eingeschlichen. Zum Testen, habe ich den Request hardcodiert - es geht mir hier erst einmal um die Funktionsweise. Ich weiss, dass ich mit WSDL etc. die Klassen generieren lassen kann. Aber ich möchte hier einfach einmal einen Service aufrufen etwas übergeben und die Antwort einlesen.

Der Fehler:
ich erhalte beim Öffnen des InputStream immer:
Server returned HTTP response code: 500 for URL: http://192.168.13.218/smsgateway/smsgatewayservice.asmx/

Der .NET-WebService funktioniert, da ich ihn mit .NET-Proxy-Klassen ohne weiteres ansprechen kann. Ich denke mein POST-String ist falsch!

Hier meine Klasse:

Code:
package de.tutorials.webservice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class ConsumeNetService {

	public static void main(String args[]) {
		System.out.println(consumeTest());
	}
	
	public static boolean consumeTest() {
		
		boolean result = false;
		
		String soapAction = "";
		String content = "";
		String header = "";
		
		//Aufgrund Anonymität
		String url = "http://tempuri.org";
		
		soapAction = "getCustomerInfo";
		
		content = "" + 
			"<?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>" + 
					"<getCustomerInfo xmlns=\"" + url + "/smsgateway/\">" + 
						"<customer>" + 
							"<AccountID>int</AccountID>" + 
							"<Username>string</Username>" + 
							"<MonthlyBill>boolean</MonthlyBill>" + 
						"</customer>" + 
					"</getCustomerInfo>" + 
				"</soap:Body>" + 
			"</soap:Envelope>";
		
		
		header = "" + 
			"POST /smsgateway/smsgatewayservice.asmx HTTP/1.1\r\n" + 
			//"Host: 192.168.13.218\r\n" + 
			//"Content-Type: text/xml; charset=utf-8\r\n" + 
			//"Content-Length: " + content.length() + "\r\n" + 
			//"SOAPAction: \"" + url + "/smsgateway/" + soapAction +"\"\r\n"; 
			//"SOAPAction: \"" + url + "/smsgateway/" + soapAction +"\"\r\n";
			"SOAPAction: \"" + url + "/smsgateway/\"\r\n";
		

		
		//String request = header + "\r\n" + content;
		String request = content;
		
		
		try {
			//URL sslURL = new URL("http://192.168.13.218/smsgateway/smsgatewayservice.asmx/" + soapAction);
			URL sslURL = new URL("http://192.168.13.218/smsgateway/smsgatewayservice.asmx/");
			
			HttpURLConnection conn = (HttpURLConnection) sslURL.openConnection();
			
			conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
			conn.setRequestProperty("Content-Length", String.valueOf(content.length()));
			conn.setDoOutput(true);
			conn.setDoInput(true);

			PrintWriter out = new PrintWriter(conn.getOutputStream());
			// Verbindung zustande gekommen
			if ((conn != null) && (out != null)) {

				try {			
					
					// Request senden
					System.out.println(request);

					char[] buffer = new char[1024 * 10];
					buffer = request.toCharArray();
					out.write(buffer, 0, request.length());
					out.close();
					
					// Response lesen
					BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		
					String inputLine = null;
					String response = "";
		
					while ((inputLine = in.readLine()) != null)
						response += inputLine;
					in.close();
					
					System.out.println(response);

					result = true;
				} catch(IOException ioe) {
					ioe.printStackTrace();
				}
				
			} else {
				// Fehlerhafte Verbindung
			}
		} catch (MalformedURLException mue) {
			mue.printStackTrace();
			result = false;
		} catch (IOException ioe) {
			ioe.printStackTrace();
			result = false;
		}
		return result;
	}
}


Code:
Und hier die SOAP 1.1 Beschreibung:
POST /smsgateway/smsgatewayservice.asmx HTTP/1.1
Host: 192.168.13.218
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/smsgateway/getCustomerInfo"

<?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>
    <getCustomerInfo xmlns="http://tempuri.org/smsgateway/">
      <customer>
        <AccountID>int</AccountID>
        <Username>string</Username>
        <MonthlyBill>boolean</MonthlyBill>
      </customer>
    </getCustomerInfo>
  </soap:Body>
</soap:Envelope>


und hier die .NET-Definition:
Code:
    <WebMethod(Description:="<b>retrieve info about Customer</b>")> _
    Public Function getCustomerInfo(ByVal customer As Customer) As String
        Return customer.Username
    End Function


In erster Linie geht es mir nur daram, das Ganze zum Laufen zu bringen. Dann kann ich noch immer dem Ganzen Sinn geben :)

ich danke Euch
SiS
 
Hi Tom

Danke für den Link zu JaxWS. Doch ich würde gerne im ersten Erguss das ganze ohne fremde Klassen einmal implementieren.

Ich denke so wild kann es ja nicht sein, eine SOAP-Envelop via POST an den .NET-Service zu schicken, aber irgendwie klappt das mit meinem Source nicht.

Ich habe übrigens in der web.config

Code:
   <system.web>
      <compilation debug="true" strict="true" explicit="true"/>
         <webServices>
            <protocols>
               <add name="HttpPost"/>
               <add name="AnyHttpSoap"/>
               <add name="HttpGet"/>
               <add name="HttpSoap"/>
               <add name="HttpSoap12"/>
               <add name="HttpPostLocalhost"/>
         </protocols>
      </webServices>
   </system.web>
alles hinzugefügt - und trotzdem erhalte ich HTTP Error 500 (.NET 2.0).

Natürlich könnte auch was mit dem .NET-WebService krumm sein, jedoch habe ich ein .NET Client-Projekt gestartet, dort den Web-Reference hinzugefügt und alles klappt.
Kann es sein, dass das hartkodierte SOAP-XML verwurstelt ist?


Ich werde mal das ganze in .NET probieren - ebenfalls ohne WSDL - mal schauen was da passiert!

cheerioh
SiS
 
Zuletzt bearbeitet:
gelöst, aber wieso?

Morgen zusammen!

Frischer Kopf am Morgen und der Fehler ist gefunden.

ich habe soviele Möglichkeiten durchgespielt, wieder rauskommentiert etc, dass ich den ganzen Code verwurstelt habe.

Was war falsch, wo waren Fehler?

-> SOAPAction nicht vergessen UND auf Grosskleinschreibung achten!
Code:
   conn.setRequestProperty("SOAPAction", url + "/smsgateway/" + soapAction);

--> URL auf die .asmx setzen UND kein Slash (/) nach .asmx!
Code:
URL sslURL = new URL("http://192.168.13.218/smsgateway/smsgatewayservice.asmx");

--> Soap-Envelope überprüfen: Werte, wohlgeformt, gültig!
Code:
"<customer>" + 
   "<AccountID>int</AccountID>" + 
   "<Username>string</Username>" + 
   "<MonthlyBill>boolean</MonthlyBill>" + 
"</customer>" +
Ich hab hundert mal den Envelope von der Dienstbeschreibung kopiert aber irgendwann einmal habe ich die Werte nicht mehr gesetzt! und wenn "int" steht, sollte man schon einen int-Wert reinsetzen!


und dann geht es ganz simpel:
Code:
package de.tutorials.webservice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class ConsumeNetService {

	public static void main(String args[]) {
		System.out.println(consumeTest());
	}
	
	public static boolean consumeTest() {
		
		boolean result = false;
		
		String soapAction = "";
		String content = "";
		
		// Anonymität
		String xmlns = "http://tempuri.org";
		String asmx = "smsgatewayservice.asmx";
		String serviceURL = "http://127.0.0.1";
		String serviceLoc = "smsgateway";
		
		soapAction = "getCustomerInfo";
		
		content = "" + 
			"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + 
			"<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/\">\r\n" + 
				"<soap:Body>\r\n" + 
					"<getCustomerInfo xmlns=\"" + xmlns + "/" + serviceLoc + "/\">\r\n" + 
						"<customer>\r\n" + 
							"<AccountID>10</AccountID>\r\n" + 
							"<Username>Homer</Username>\r\n" + 
							"<MonthlyBill>True</MonthlyBill>\r\n" + 
						"</customer>\r\n" + 
					"</getCustomerInfo>\r\n" + 
				"</soap:Body>\r\n" + 
			"</soap:Envelope>";
				
		try {
			// URL zum Web-Service
			URL sslURL = new URL(serviceURL + "/" + serviceLoc + "/" + asmx);
			HttpURLConnection conn = (HttpURLConnection) sslURL.openConnection();
			
			// Header-Attribute
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
			conn.setRequestProperty("SOAPAction", xmlns + "/" + serviceLoc + "/" + soapAction);
			conn.setRequestProperty("Content-length", String.valueOf(content.length()));
			conn.setDoOutput(true);
			conn.setDoInput(true);
			
			// Verbindung öffnen
			OutputStream out = conn.getOutputStream();

			// Verbindung zustande gekommen?
			if ((conn != null) && (out != null)) {

				try {
					// Request senden
					byte[] bytes = content.getBytes();			
					out.write(bytes);
					out.close();
					
					// Response einlesen
					BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

					String inputLine;
					String response = "";
					while ((inputLine = in.readLine()) != null)
						response += inputLine;
					in.close();
					
					System.out.println(response);

					result = true;
				} catch(IOException ioe) {
					ioe.printStackTrace();
				}
				
			} else {
				// Fehlerhafte Verbindung
				System.err.println("nada Verbindung!");
			}
		} catch (MalformedURLException mue) {
			mue.printStackTrace();
			result = false;
		} catch (IOException ioe) {
			ioe.printStackTrace();
			result = false;
		}
		return result;
	}
}

Dank und Gruss
SiS
 

Neue Beiträge

Zurück