I have an .net 2.0 C# client app that has a web service reference to an Axis2 Java Webservice.
The idea is to send some xml data to the webservice, so it can be parsed and inserted into database.
The WS method accepts a single parameter of type 'xsd:anytype'.
Java web service:
public class JWS{
public Response AddData(Object inputXML) {
return Response;
}
}
C# Client:
JWS client = new JWS();
object inputXML = "<xml>some xml data</xml>";
response = client.AddData(inputXML);
There are 2 issues i am seeing when monitored using fiddler.
1) The request has an additional element '<inputXML>' added before the actual xml data.
<inputXML><xml>some xml data</xml></inputXML>
2) The xml is encoded, so '<' is appearing as "<"
I am not sure if this is how SOAP request's are generated but i would like to remove the <inputXML> tag and also, have the xml appear as is without having to replace the special characters.
Is this possible? Is it got something to do with 'Wrapping'/'UnWrapping' Types?
Also, i have used SoapUI to test the java web service and it works well. However, in the request tab, i had to manually remove the <inputXML> tag and submit for it to work correctly. Please help.
TIA
This is expected behaviour under SOAP and the inputXml variable will be decoded back to the original string when passed to your web service method.
However this may indicate a problem with your design, have you considered constructing an object to send to your web service rather than xml data? (As this object will transparently be converted to xml for the web service call anyway).
I found out that the issue is not with encoding but it was interpreted incorrectly on java side when the message was viewed in axis2. So, it is getting decoded properly. Also, the inputxml is now being handled correctly.
Related
I'm trying to write an ASMX web service to receive a block of JSON data from elsewhere (meaning I have no control over the format of the data - it's documented and consistent, but outside my control). As a test, I've created a simple webpage that sends that same data via an AJAX request. The data is definitely attached as the payload of my POST request:
Picture of chrome devtools showing payload
But when I try to receive that data in my ASMX webservice, I get an empty string:
[WebMethod]
public string UpdateProjectImage()
{
using (var sr = new System.IO.StreamReader(HttpContext.Current.Request.InputStream))
{
string json = sr.ReadToEnd(); // this comes out as an empty string!
return json;
}
}
Please note that this is being built with VS 2012 and IIS 7.5 running on Win2008R2, and I cannot change the technology stack.
The streamreader technique ought to work, because I copied it from another webservice that's working - but it doesn't work in this case (it just gives me an empty string), and I don't know why. It's probably some weird configuration setting, but I just don't know what setting it might be.
Apparently I have to set sr.BaseStream.Position = 0 before the call to sr.ReadToEnd(). I'm not 100% it's the "right" solution (I've done this elsewhere and I didn't need to set the position, and it just seems weird to have to do that), but it's working now and that's good enough for me.
Special thanks to Jasen for pointing me in the right direction.
My parent company has a WSDL, and I can create a ServiceModel, and use it to invoke web methods, and that's fine. However, what I need is to generate the SOAP XML without sending it. So, if I do this:
var mySvc = new MyWsdl.MyWebService();
var response = mySvc.DoTheThing(myUser, myData, myOtherData);
I know that under the covers, .NET is creating XML and sending it across the wire. But I need that XML without sending it, so, ideally, some code like:
var mySvc = new MyWsdl.MyWebService();
var xml = mySvc.GetSOAP("DoTheThing", myUser, myData, myOtherData);
Is there a way to do this? I am trying to get out of manually generating the XML.
The reason I'm doing this is because I need to actually send the SOAP to a different, normal non-SOAP web service. I suspect that that service is just going to turn around and call the SOAP service anyway, possibly massage the results, and return something to me, but that's out of my hands. Very soon I will not be able to use the ServiceClient directly.
Thanks!
EDIT: I would like to do this programatically. Otherwise, yes, I can get the XML by capturing a sent request, or using an app like SoapUI, but then I'm still manually creating the XML. The idea is, that if the WSDL changes, I don't want to have to change the XML I generate, so if the service client can do it for me, that would be ideal.
I have a WCF service that is being used to generate an XML file based on multiple different queries. In the end I am left with a complete XML file and I need a way of passing this back to the application that calls the method in my service. How can this be achieved using WCF?
I have tried a multitude of things such as sending back an XmlElement instead and populating that but when I do that the best I can do is pass back the root element and the contents inside that which isnt ideal as I lose the header which I need.
I tried this:
[OperationContract]
XmlElement Foo(MyType myType, string user);
string responseXMLString = getPointsResponse.ResponseHeader;
responseXMLString += getPointsResponse.ResponseRecords;
responseXMLString += getPointsResponse.ResponseFooter;
XmlDocument myDocument = new XmlDocument();
myDocument.LoadXml(responseXMLString);
return myDocument.DocumentElement;
This got me the whole document minus the header but I need the header. I want to send it all back as one object in XML format.
When I tried to send back an XML document I got a multitude of errors. When I also try to send it back as a string I get errors due to it having special chars and interfering with the SOAP response.
Any ideas?
Out of the box WCF's http-based bindings all use soap to wrap the message payload except webHttpBinding, which enables support for RESTful-style interfaces.
Alternatively you could be looking at is how to achieve POX messaging with WCF, which can be found here: http://msdn.microsoft.com/en-us/library/aa395208(v=vs.90).aspx
UPDATE
REST support in WCF has well established procedures for security. For example, http://www.codeproject.com/Articles/149738/Basic-Authentication-on-a-WCF-REST-Service
Additionally I would say that you should look at your service contract composition, ie the number and types of operations exposed on your endpoint. It may be that this problem you face is a good enough reason to decouple the POX operations from the SOAP operations into their own service endpoint.
You can get it in string format using the following.
return myDocument.DocumentType.ToString() + myDocument.DocumentElement;
I've built a simple C# app (.Net 4.0 + WPF) which can send and receive JSON messages via TCP sockets.
As a next step, it should be possible that JavaScript apps on websites and PHP scripts can send and receive JSON messages to/from my app. Is that possible?
Since JS/PHP will use stateless HTTP connections, how should a request to my app work, for example, should the JS/PHP apps send a JSON message to my app and my app response (HTTP response) with a JSON message? Is that even possible? And should I use GET or POST method to send the JSON messages to/from my app?
Hope my questions do not cause too much confusion ;-) I but I appreciate every tip, clarification or feedback you can give me.
Mike
You can accomplish this via a .NET web service using special JSON directives on the web method, e.g.
[ScriptMethod(UseHttpGet = true, ResponseFormat=ResponseFormat.Json)]
public string DoSomething(string param1, int param2)
{
// Do Something
}
When the ResponseFormat.Json property is specified, the data returned will be serialized into the appropriate JSON format. Also note, in order to recieve a true JSON response, you'll need to set your content-type to "application/json" from the requesting application. Otherwise, the method will attempt to wrap the response in XML.
Also, I am enabling a HttpGet on this method so that you can post via a query string to the method, e.g.
http://www.example.com/service.asmx?param1='Hello'¶m2=1;
I have a quasi-RESTful ASP.NET web service that I wish to be able to post XML to. My method looks something like this:
[WebMethod(false, System.EnterpriseServices.TransactionOption.NotSupported)]
public void save(string saveXml)
{
XDocument saveXml = XDocument.Parse(saveXml);
....
When I try to post to that web service, I get the exception:
System.Web.HttpRequestValidationException: A potentially dangerous Request.Form
value was detected from the client...
Based on some searching, my understanding is that all requests to ASP.NET pages look for characters like "<" which are deemed dangerous (obviously this applies to my XML). This can be turned off easily for individual pages, but not web services. The only solution I have found involved changing the request validation mode in my web.config to "2.0". I don't want to change to an old version of something just because my one web service method doesn't work with the 4.0 version. Is there any way to disable this for my specific method? Thanks in advance.
The new 4.0 RequestValidation model is a bit pickier, but as long as your client sends the HTTP request Content-type header as text/xml instead of application/x-www-form-urlencoded it should pass (or bypass rather) ASP.NET RequestValidation.