Can I post XML data to a Restfull service - c#

I have an application that will be posting an XML object to me.
Is it possible to create a Restful service (I'm assuming using Win API) to post the data to, or should I be using WCF services?

You can do this with WebAPI by just reading the content stream from the request:
public XmlDocument ReadRawXml(HttpRequestMessage request)
{
var xmlDoc = new XmlDocument();
xmlDoc.Load(request.Content.ReadAsStreamAsync().Result);
return xmlDoc;
}
You can call that in your WebAPI code.

Related

Get XML of Request and Response to a web service

In c# using Asp.net, I am trying to get the XML format of the request and response I am making to a web service. I used one of the solution from this site but it is incomplete in my situation. I am able to serialize the response but I cannot do the same for the request since I am sending in multiple parameters as request and I cannot completely figure this out.
Example :
RefreshInfo info = refreshclient.getRefreshResponse(refreshcontext, itemID);
I added the below code for response and I could see the XML format of it.
XmlSerializer xmlSerializer = new XmlSerializer(info.GetType());
using (StringWriter textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, info );
string repsonsexml = textWriter.ToString();
}
How can I do the same for the request from this?
Help is appreciated.. Thank you!!
I think the answer is right here. Posted too early, I suppose!
Generating XML from multiple classes?

Web Service to get XML from client in C#

I want to get an XML response from Client by using Web service.For this i am going to create a web service that can take XML , so that they can use the web service to send the XML to me.
Remember in this i have to create both ends.
Somebody please advise on this...
public XmlDocument GetXmlDocument(string pXML)
{
// Create an XmlDocument object.
XmlDocument xmlDocumentObject = new XmlDocument();
xmlDocumentObject.LoadXml(pXML);
// Return the created XmlDocument object.
return (xmlDocumentObject);
}
The above code will take take any XML as string . How could my client will use this method on there end and send the response to me.

How to Capture incoming SOAP XML Message (WSDL format) from external web service

I am new to web services and need to capture SOAP XML messages that will be sent to my web service. I found article that says you can read the contents of the Request.InputStream from within your asmx WebMethod.
Capturing SOAP requests to an ASP.NET ASMX web service
Code is as follows:
using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using System.IO;
using System.Text;
using System.Web.Services;
using System.Web.Services.Protocols;
namespace SoapRequestEcho
{
[WebService(
Namespace = "http://soap.request.echo.com/",
Name = "SoapRequestEcho")]
public class EchoWebService : WebService
{
[WebMethod(Description = "Echo Soap Request")]
public XmlDocument EchoSoapRequest(int input)
{
// Initialize soap request XML
XmlDocument xmlSoapRequest = new XmlDocument();
// Get raw request body
Stream receiveStream = HttpContext.Current.Request.InputStream
// Move to begining of input stream and read
receiveStream.Position = 0;
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
// Load into XML document
xmlSoapRequest.Load(readStream);
}
// Return
return xmlSoapRequest;
}
}
}
However, I am confused because this asks for an int input parameter. I suppose I could just remove it, but I am not sure how the external user would call my web service and post an XML message to it. How could I perform a test on this to send it XML messages and make sure I can capture them in the stream? Any tips or links would be greatly appreciated, thanks.
"I am new to web services and need to capture SOAP XML messages that will be sent to my web service."
SOAP is just a protocol for exchanging information between the consumer of the webservice and the webservice. Consider it a handshake of sorts where the data to be passed to the web service is packaged into a SOAP envelope. The consumer of the webservice will send you the data packed into a SOAP envelope. This begs the question - How will the consumer know what to send?
When you open your webservices page it should show you a list of operations supported. If you click on EchoSoapRequest you will see a sample SOAP request that should be sent to your service and its response. All you need to do is handle the parameters that the user sends in your code.
In this case your webservice expects an int. The user who consumes your web service will send an int packed within the SOAP envelope. If you want your user to send a string then declare a string as the input.
PS: As a side note you should look at RESTful web services and their advantages if you are starting from scratch.

Posting soap xml to a web service directly in c#/ asp.net

I am trying to post XML to a web service directly rather than using VS's "add a web reference..." functionality. There is a good reason for this. To get proper test XML I logged what was generated using the standard web reference. I used what I found in this question Capturing SOAP requests to an ASP.NET ASMX web service
The web service was just what is generated when you create a new web service in visual studio, nothing fancy.
To try and pass the XML itself, I borrowed the code from this question Client to send SOAP request and received response
Unfortunately, when I call it, I get "The remote server returned an error: (500) Internal Server Error." 100% if I call the same web method like this
WebService1 ws = new WebService1();
String output = ws.HelloWorld();
then everything works great so I know the service is working well as deployed. I would appreciate so greatly some advice as to what I am doing wrong =)
Here is my code:
var _url = "http://localhost/simulator/webservice1.asmx";
var _action = "http://localhost/simulator/webservice1.asmx?op=HelloWorld";
String soapMessage = soapMessage = #"<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Body><HelloWorld xmlns='http://tempuri.org/' /></soap:Body></soap:Envelope>";
XmlDocument soapEnvelop = new XmlDocument();
soapEnvelop.LoadXml(soapMessage);
XmlDocument soapEnvelopeXml = soapEnvelop;
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
// begin async call to web request.
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
// suspend this thread until call is complete. You might want to
// do something usefull here like update your UI.
asyncResult.AsyncWaitHandle.WaitOne();
// get the response from the completed web request.
string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}

How to modify webservice proxy to get Raw XML

Here's the proxy method that was created for the web service I'm trying to access. How would I go about modifying it to get the raw XML from the web service call?
/// <remarks/>
[System.Web.Services.Protocols.SoapHeaderAttribute("CallOptionsValue")]
[System.Web.Services.Protocols.SoapHeaderAttribute("MruHeaderValue")]
[System.Web.Services.Protocols.SoapHeaderAttribute("SessionHeaderValue")]
[System.Web.Services.Protocols.SoapHeaderAttribute("QueryOptionsValue")]
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestNamespace = "urn:partner.soap.sforce.com", ResponseNamespace = "urn:partner.soap.sforce.com", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
[return: System.Xml.Serialization.XmlElementAttribute("result")]
public QueryResult query(string queryString)
{
object[] results = this.Invoke("query", new object[] {
queryString});
return ((QueryResult)(results[0]));
}
Thanks for your help!
Fortunately there is a nice way to do it, just modify the generated proxy class so it inherits from different base. The alternative implementation comes from Web Services Enhancements 3.0 pack:
Microsoft.Web.Services3.WebServicesClientProtocol
in the class you'll have RequestSoapContext.Envelope.InnerXml and ResponseSoapContext.Envelope.InnerXml in the scope - that's exactly what you need.
If you want just make a dump using the Fiddler Web Debugging tools.
If you want to really retrive/process raw XML then proxy method will not help you. Create System.Net.HttpWebRequest for the web service, call it, and retrive pure XML response. Format/structure can be found at .ASPX page or web service documentation.
Well, as far as I remember, a buddy of mine that this once with regular ASMX webservices, and it was quite a hack....
How about just serializing the result you get back into XML? That might do the trick...
Something along the lines of (totally untested and from memory):
MemoryStream stm = new MemoryStream();
XmlSerializer xmlSer = new XmlSerializer(typeof(QueryResult));
xmlSer.Serialize(stm, queryResult);
Would that work?
Marc

Categories

Resources