Fra java til .NET
Jeg har fået til opgave at konvertere denne java metode til c# metode.Det er en soap web service med en vedhæftet fil der bliver generet. Kan man lave en tilsvarende metode i c#?
private static void sendSoapMessage( String SOAPPartText, String Payload )
{
boolean debugMode = true;
try
{
loadProperties();
// Create a new SOAP message from the factory and set the SOAPAction header.
MessageFactoryImpl factory = new MessageFactoryImpl();
SOAPMessage soapMsg = factory.createMessage();
MimeHeaders mimeHeaders = soapMsg.getMimeHeaders();
mimeHeaders.addHeader( "mockResponseType", props.getProperty("mockResponseType") );
sendCompressedResponse = props.getProperty("sendCompressedResponse");
mimeHeaders.addHeader( "sendCompressedResponse", sendCompressedResponse );
mimeHeaders.addHeader( "Cookie", "JSESSIONID="+props.getProperty("JSESSIONID") );
// Set the SOAP envelope contents to our ebXML file.
SOAPPart part = soapMsg.getSOAPPart();
part.setContent( new StreamSource( new StringReader( SOAPPartText )));
// The Payload needs to be loaded as a DOM object, and then added
// as the attachment part of the message.
DOMSource domSource = new DOMSource( loadDocument( new StringReader( Payload )));
AttachmentPart ap = soapMsg.createAttachmentPart( domSource, soapMimeType );
ap.setContentId( otaContentID );
ap.setContentType( soapMimeType );
soapMsg.addAttachmentPart( ap );
if (debugMode == true)
{
System.out.println("***************** BEGIN SOAP REQUEST: *********************:");
// for debugging, write the request object to the console.
soapMsg.writeTo( System.out );
System.out.println("***************** END SOAP REQUEST: *********************:");
}
// Create a SOAPConnection to send the SOAP request on.
SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance();
SOAPConnection con = conFactory.createConnection();
// Point to the service we want to call, and then call it. Store the response
// in the "reply" object.
URLEndpoint endPoint = new URLEndpoint( props.getProperty("endPoint") );
long callStart = System.currentTimeMillis();
SOAPMessage reply = con.call( soapMsg, endPoint );
long totalTime = (System.currentTimeMillis() - callStart)/1000;
if ( debugMode == true )
{
// for debugging, write the response object to the console.
//System.out.println("***************** BEGIN SOAP REPLY: *********************:");
//reply.writeTo( System.out );
//System.out.println("***************** END SOAP REPLY: *********************:");
}
// Create two unique filenames for the SOAP and Payload output.
String soapPartFile = createFileName( SOAPPartFileName, otaActionID );
String payloadFile = createFileName( PayloadFileName, otaActionID );
// Write the SOAPPart to the file.
DataHandler dataHandlerSOAPPart = new DataHandler(reply.getSOAPPart().getContent(), soapMimeType);
writeToFile(dataHandlerSOAPPart, soapPartFile);
// Get each Payload attachment and write it to the file.
Iterator attItr = reply.getAttachments();
while(attItr.hasNext())
{
AttachmentPart attPart = (AttachmentPart)attItr.next();
DataHandler attDataHandler = attPart.getDataHandler();
if( sendCompressedResponse.equalsIgnoreCase("true") )
{
DataSource ds = attDataHandler.getDataSource();
ByteArrayInputStream bin = (ByteArrayInputStream) ds.getInputStream();
byte[] b = new byte[bin.available()];
bin.read(b);
writeToFile(b, payloadFile);
} else {
writeToFile(attDataHandler, payloadFile);
}
}
System.out.println("\nCall to endpoint "+ props.getProperty("endPoint") + " took: " +
totalTime + " sec(s)\n");
// Close the SOAP connection.
con.close();
}
catch (Exception e)
{
System.out.println( "Problem sending the SOAP message" );
System.out.println( e.toString() );
}
}