问题描述:

My web application has a HttpHandler that takes in some XML (request message) via HTTP POST, that does something with it and that then returns some other XML back (response message). But one of the parties that is sending me the XML is now insisting they do this over SOAP in stead of HTTP POST. The fact that it is becoming SOAP is not very important, it just adds some tags around the initial payload. Let's just receive the data, transform the message back into what it was before, and deliver it to the original HttpHandler...

So first, the message that I used to get in my HttpHandler was like

<placeorder>

<products>

<product>123</product>

<product>123</product>

</products>

</placeorder>

And now the message that I get in my SoapService is like

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

<soapenv:Header/>

<soapenv:Body>

<placeorder>

<products>

<product>123</product>

<product>123</product>

</products>

</placeorder>

</soapenv:Body>

</soapenv:Envelope>

In order not to change too much code, I thought about just writing a method placeorder in a SoapService asmx, that has a parameter products. It will fake a HttpRequest and pass it to my original HttpHandler.

public class SoapService : WebService

{

[WebMethod]

public XmlDocument placeorder(XmlDocument products)

{

var hrq = new HttpRequest(null, "http://dummy/dummy", null);

var requestString = "<placeorder><products>";

requestString += products.DocumentElement.OuterXml;

requestString += "</products></placeorder>";

/* this line is failing */ hrq.InputStream.Write(Encoding.UTF8.GetBytes(requestString), 0, requestString.Length);

var ms = new MemoryStream();

var sw = new StreamWriter(ms);

var hrs = new HttpResponse(sw);

var hc = new HttpContext(hrq, hrs);

var mhh = new MyHttpHandler();

mhh.ProcessRequest(hc); // finally execute my original code

var responseString = hc.Response.OutputStream.ToString();

var externalResponse = new XmlDocument();

externalResponse.LoadXml(responseString);

return externalResponse;

}

}

This all works except for the part where the payload (requestString) is written to the InputStream of the faked HttpRequest. The stream does not support writing to it. BUt I also cannot set the stream when constructing the HttpRequest. And I cannot use another stream to replace it, since the property is read-only.

So how can I accomplish setting the InputStream of creating a HttpRequest?

相关阅读:
Top