Manually deserialize WebRequest XML - serialization

SO I have an ASMX web service that returns a array of Search Result objects. When I call the WebMethod via the browser, the following XML is generated...
<?xml version="1.0" encoding="utf-8"?><ArrayOfSearchResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/"><SearchResult>
<Name>Rock Salt Steak House</Name>
<BusinessType>Steakhouses</BusinessType>
<Rating>mStar30</Rating>
<Address>1232 Westlake Ave N</Address>
<City>Seattle</City>
<State>WA</State>
<Phone>(206) 284-1047</Phone>
<Zip>98109</Zip></SearchResult><SearchResult>
<Name>Laredos Grill</Name>
<BusinessType>Tex-Mex Bars</BusinessType>
<Rating>mStar35</Rating>
<Address>555 Aloha St Ste 100</Address>
<City>Seattle</City>
<State>WA</State>
<Phone>(206) 218-1040</Phone>
<Zip>98109</Zip></SearchResult>.......
This XML gets saved in a file to be de-serialized later. The problem is I can't seem to get it serialized again. Here's the code I use....
XmlSerializer serializer = new XmlSerializer(typeof(List<Service.SearchResult>));
using (StringReader stringReader = new StringReader(strXMLContent)) // can throw ArgumentNullException
{
using (XmlReader xmlReader = XmlReader.Create(stringReader))
{
//xmlReader.Read();
return ((List<Service.SearchResult>)serializer.Deserialize(xmlReader)).ToArray(); // can throw SerializationException
}
}
The error I get is complaining about "there is an error in xml document (2,2)" and the inner exception is (InnerException = {"ArrayOfSearchResult xmlns='http://tempuri.org/' was not expected."})
Of course when calling this WebMethod in code the collection comes down easily. It isn't until I try to manually deserialize later that it's get's mad.
Any ideas would be greatly appreciated...
Thanks!

First, why are you calling it via WebRequest? Why not just use "Add Service Reference" and use the proxy class?
Secondly, when you're calling it in the browser, you're not using SOAP. Note the lack of SOAP Envelope in the response.

Related

WCF Change message encoding from Utf-16 to Utf-8

I have a WCF connected service in a .net core application. I'm using the code that is autogenerated taken the wsdl definition.
Currently at the top of the request xml is including this line:
<?xml version="1.0" encoding="utf-16"?>
I can't find a simple way to change this encoding to UTF-8 when sending the request.
Since I could find a configuration option a the request/client objects, I've tried to change the message with following code at IClientMessageInspector.BeforeSendRequest
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
// Load a new xml document from current request
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(request.ToString());
((XmlDeclaration)xmlDocument.FirstChild).Encoding = Encoding.UTF8.HeaderName;
// Create streams to copy
var memoryStream = new MemoryStream();
var xmlWriter = XmlWriter.Create(memoryStream);
xmlDocument.Save(xmlWriter);
xmlWriter.Flush();
xmlWriter.Close();
memoryStream.Position = 0;
var xmlReader = XmlReader.Create(memoryStream);
// Create a new message
var newMessage = Message.CreateMessage(request.Version, null, xmlReader);
newMessage.Headers.CopyHeadersFrom(request);
newMessage.Properties.CopyProperties(request.Properties);
return null;
}
But the newMessage object still writes the xml declaration using utf-16. I can see it while debugging at the watch window since.
Any idea on how to accomplish this (should be) simple change will be very apreciated.
Which binding do you use to create the communication channel? The textmessageencoding element which has been contained in the CustomBinding generally contains TextEncoding property.
https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/wcf/textmessageencoding
WriteEncoding property specifies the character set encoding to be used for emitting messages on the binding. Valid values are
UnicodeFffeTextEncoding: Unicode BigEndian encoding
Utf16TextEncoding: Unicode encoding
Utf8TextEncoding: 8-bit encoding
The default is Utf8TextEncoding. This attribute is of type Encoding.
As for the specific binding, it contains the textEncoding property too.
https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.basichttpbinding.textencoding?view=netframework-4.0
Feel free to let me know if there is anything I can help with.

BizTalk 2010 WCF Remove processing instruction

I need to do download an XML file from a public website (http://www.tcmb.gov.tr/kurlar/201707/10072017.xml) to get exchange rates.
But I have a problem since the XML contains an xml-stylesheet processing instruction.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="isokur.xsl"?>
<Tarih_Date Tarih="07.07.2017" Date="07/07/2017" Bulten_No="2017/131" >
I use a WCF-Custom port with webHttpBindng and BizTalk REST starter kit from bLogical. Everything works fine, but when I try to parse the incoming xml, I get an error on that processing instruction.
System.Xml.XmlException: Processing instructions (other than the XML declaration) and DTDs are not supported. Line 2, position 2.
I'm not sure what the best way would be to fix this. I tried to follow this guide WCF Errors on XML Deserialization but it still fails when I try to access the message content using the CreateBufferedCopy method.
using (var readStream = new System.IO.MemoryStream())
{
using (var buffer = reply.CreateBufferedCopy(int.MaxValue))
{
buffer.WriteMessage(readStream);
}
readStream.Position = 0;
xdoc.Load(readStream);
}
Does anybody know how I can access the content of my message without actually parsing the XML? I'm just trying to find a way to either remove that line or make the parser ignore it.
I found the solution myself in the end. Instead of a message inspector, I created a Message Encoder based on the CustomTextMessageEncoder that you can find online.
In the ReadMessage method I just added a little bit of code
public override Message ReadMessage(System.IO.Stream stream, int maxSizeOfHeaders, string contentType)
{
XmlReaderSettings xsettings = new XmlReaderSettings();
xsettings.IgnoreProcessingInstructions = true;
XmlReader reader = XmlReader.Create(stream,xsettings);
return Message.CreateMessage(reader, maxSizeOfHeaders, this.MessageVersion);
}

VBA MSSOAP.SoapClient30 error: Incorrect number of parameters supplied for SOAP request HRESULT=0x80070057

update: so I've figured I need to somehow submit a complex type at method parameter - how do I do this with VBA?
This is my first time coding VBA and I will appreciate any possible pointers at how I can fix the problem. Basically, I've written a little soap service and it works fine - I test it with SoapUI - so I guess other application should be able to consume it.
The WSDL the service generates is here. Perhaps, it is not too friendly for consuming by VBScript SOAPClient - any points in that direction will help a lot.
I'm trying to put a bit of code together that actually uses it (VBScript below) - I've built it on top of an example I found while googling. It generates the following error:
Incorrect number of parameters supplied for SOAP request HRESULT=0x80070057
Module Module1
Dim WSDLFileName As String
Dim base64attachment As String
Dim attachment_filename As String
Dim summary As String
Dim SoapClient
Dim res
Sub Main()
WSDLFileName = "http://localhost:7777/?wsdl"
base64attachment = "UG9ydG1hbiBpcyBwb3J0Zm9saW8gbWFuYWdlbWVudCBzb2Z0d2FyZSB0byBoZWxwIFBNTyBrZWV"
attachment_filename = "test_file.txt"
summary = "test issue with summary"
SoapClient = CreateObject("MSSOAP.SoapClient30")
SoapClient.MSSoapInit(WSDLFileName)
res = SoapClient.CreateJiraIssueWithBase64Attachment(summary, base64attachment, attachment_filename)
Console.Out.WriteLine(res)
End Sub
End Module
Any pointers will help, I'm lost here.
I'm expecting it should create a response like this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:open="open.JiraAdapter">
<soapenv:Header/>
<soapenv:Body>
<open:CreateJiraIssueWithBase64Attachment>
<open:summary>some summary</open:summary>
<open:base64attachment>BASE64CODEDFILE</open:base64attachment>
<open:attachment_filename>NAME of the file attached</open:attachment_filename>
</open:CreateJiraIssueWithBase64Attachment>
</soapenv:Body>
</soapenv:Envelope>
Your service response contains complex type object.
<xs:element name="CreateJiraIssueWithBase64AttachmentResult" type="s0:Status" minOccurs="0" nillable="true"/>
To be able to use complex types you need to use "MSSOAP.SoapSerializer30" to create request and "MSSOAP.SoapReader30" for reading response.
SOAP UI can helps you to see the correct request structure (tags, namespaces and actions).
I think it something like that
Connector = CreateObject("MSSOAP.HttpConnector30")
Connector.Property("EndPointURL") = "url"
Connector.Property("UseSSL") = True
Connector.Connect
Connector.Property("SoapAction") = "CreateJiraIssueWithBase64Attachment"
Connector.BeginMessage
Serializer = CreateObject("MSSOAP.SoapSerializer30")
Serializer.Init(Connector.InputStream)
Serializer.StartEnvelope
Serializer.StartBody
Serializer.StartElement("CreateJiraIssueWithBase64Attachment";"open.jiraAdapter.test")
Serializer.StartElement("summary";"open.jiraAdapter.test")
Serializer.WriteString("another test issue for JUR")
Serializer.EndElement
Serializer.StartElement("base64attachment";"open.jiraAdapter.test")
Serializer.WriteString("Y29kZTogaHR0cDovL3Bhc3RlYmluLmNvbS9EbUx3N0oycQ0KeG1sOiBodHRwOi8vcGFzdGViaW4uY29tLzE3Q2MxVjJM")
Serializer.EndElement
Serializer.StartElement("attachment_filename";"open.jiraAdapter.test")
Serializer.WriteString("readme.txt")
Serializer.EndElement
Serializer.EndElement
Serializer.EndBody
Serializer.EndEnvelope
Connector.EndMessage
Reader = CreateObject("MSSOAP.SoapReader30")
Reader.Load(Connector.OutputStream)
/// Reader.Body.xml - response
Hope this help you.

Magento API SOAP v1 and VB.NET

I'm using VB.NET to communicate with Magento via the API SOAP v1. I had it working fine until I got to a call that needed an associative array. I've tried dictionaries, hashtables, custom types, etc. I did read hashtables and dictionaries are not serializable. So I tried the custom type/object/class. Below is the error I received:
The type XXXX was not expected. Use the Xmlinclude or SoapInclude attribute to specify types that are not known statically.
So I've read a lot of posts in regards to the above error and I can't get anything to work. The error seems at least to say what I'm trying is possible if I do it right, but maybe that is not the case. I don't know much about SOAP, but I can see this never working since the web service might not know how to handle the object.
So my question is if it's possible to send a custom object to the Magento API. Or more broadly is it possible to get SOAP v1 to work with VB.NET. I know v2 is an option but I'm pretty familiar with v1 and already having it working in VB.NET other than this scenario.
Answering a question you didn't ask — but have you considered using the XML-RPC adapter for the V1 API? It exposes the same exact methods as the SOAP API, but since XML-RPC doesn't have the same strong concept of types that SOAP does you may be able to get a generic object through from .NET (said without being that familiar with .NET SOAP or XML-RPC libraries)
It took a lot of digging, but I have it all working. To start I used the PHP SoapClient and noted how it formed associative arrays in the soap request. You can trace requests and responses, pretty handy. From there I wrote my own soap client in VB.NET using a WebRequest object. In doing so I have full control over the xml being sent to the API.
Again the reason I went down this road is ultimately V2 was not working for me. For some reason not all parameters were making it to the API. That and the fact I'm pretty comfortable with V1 too. I've written several custom APIs.
I apologize for the brevity, but there is a lot that went into this. Probably most of my time was hitting multiple dead ends. If anyone wants specifics feel free email me.
EDIT:
Here is the php code I used to see how I needed to format the requests:
$client = new SoapClient('http://www.site.com/index.php/api/soap/?wsdl',array('trace' => TRUE));
$session = $client->login('user','api-key');
echo $client->__getLastRequest() ."\n\n";
echo $client->__getLastRequestHeaders() ."\n\n";
echo $client->__getLastResponse() ."\n\n";
echo $client->__getLastResponseHeaders() ."\n\n";
$result = $client->call($session, 'cataloginventory_stock_item.list','393');
echo $client->__getLastRequest() ."\n\n";
echo $client->__getLastRequestHeaders() ."\n\n";
var_dump($result);
$client->endSession($session);
Below is how to send the request using VB.NET. You will need to construct the XML/SOAP body using the php above as a guide. I made a class per API call which output the needed XML. You will need System.Net, System.Xml & System.IO. I used getSoapHeader() because there is some common XML that goes into a request. See next code section for more details:
Private Function makeSoapRequest(ByVal soapBody As String) As String
Dim req As WebRequest = WebRequest.Create(_soap_url)
Dim xml As String
xml = getSoapHeader() & soapBody
Dim buffer() As Byte = System.Text.Encoding.UTF8.GetBytes(xml)
req.ContentType = "text/xml; charset=utf-8"
req.Method = "POST"
req.Headers.Add("SOAPAction", "urn:Mage_Api_Model_Server_HandlerAction")
req.ContentLength = buffer.Length
Dim st As System.IO.Stream = req.GetRequestStream
st.Write(buffer, 0, buffer.Length)
st.Close()
Dim response As WebResponse
Try
response = req.GetResponse
Catch ex As WebException
response = ex.Response
End Try
st = response.GetResponseStream()
Dim reader As New StreamReader(st)
Dim responseFromServer As String = reader.ReadToEnd()
makeSoapRequest = responseFromServer
response.Close()
st.Close()
End Function
Below is the getSoapHeader() function. As noted the ns2 portion is only needed if you are using type="ns2:Map" which is what I needed for associative arrays:
Private Function getSoapHeader() As String
'ns2 is not always needed
getSoapHeader = "<?xml version=""1.0"" encoding=""UTF-8""?><SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ns1=""urn:Magento"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:SOAP-ENC=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:ns2=""http://xml.apache.org/xml-soap"" SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""> " & vbCrLf
End Function

How to serialize java.sql.Date as Long, when using web services?

I have a simple web service method that returns a simple java class as return value.
#WebMethod()
public SimpleClass myMethod();
#XmlRootElement()
public class SimpleClass {
#XmlElement(name="myDate")
#XmlJavaTypeAdapter(value=MyDateAdapter.class)
public java.sql.Date myDate = new java.sql.Date(new java.util.Date().getTime());
}
I want that java.sql.Date will transmitted as Long value in the XML (because the client is J2ME that cannot handle complex things).
For this puprpose I took the solution that was mentioned in many places before, and worked great on Glassfish v2.
First, I declare the following adapter:
public class MyDateAdapter extends XmlAdapter<Long, java.sql.Date> {
public java.sql.Date unmarshal(Long v) throws Exception {
return new java.sql.Date(v);
}
public Long marshal(java.sql.Date v) throws Exception {
return v.getTime();
}
}
Then, I declare its usage in package-info file like this:
#XmlJavaTypeAdapters({
#XmlJavaTypeAdapter(value=MyDateAdapter.class,type=java.sql.Date.class)
})
package mingler.tracker.ejb.client;
The problem happens on GlassFish 3. The date is transmitted as "xs:dateTime" value, instead of Long, although I defined the adapter properly.
This is the response I get from GlassFish 3 server, when I call my web service:
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:myMethodResponse xmlns:ns2="http://nevermind.com">
<return>
<myDate xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:dateTime">2010-12-09T12:44:06.875+02:00</myDate>
</return>
</ns2:myMethodResponse>
</S:Body>
</S:Envelope>
I also checked with the debugger -- the functions in the adapter are never called.
On the other hand, the adapater is not useless, because if I try to remove it I get JAXB exception,
telling me that java.sql.Date cannot be handled because it doesn't have no-arg constructor.
Any ideas?
20/12/2010 -
I added links to a project jar with sources and the results for glassfish2 and glassfish3:
jar file ,glassfish3 result , glassfish2 result
I have posted a bug for this issue, it should be solved on version 3.1 (link)
You have the #XmlJavaTypeAdapter declared at both the property level and the package level. Have you tried only declaring it in one spot?
Either:
#XmlJavaTypeAdapter(value=MyDateAdapter.class)
public java.sql.Date myDate = new java.sql.Date(new java.util.Date().getTime());
Or:
#XmlJavaTypeAdapters({
#XmlJavaTypeAdapter(value=MyDateAdapter.class,type=java.sql.Date.class)
})
package mingler.tracker.ejb.client;
Instead of both.