Strange Characters from Web Service Output - Data Dependent...
Hi All
I'm busy writing an Application that makes extensive use of Web Services. Everything seems to be working fine, aside from "non-standard" characters appearing as question marks depending on the size of the return object.
I can see a value come through containing an apostrophe. In Wireshark it shows as "\342\200\231", and when pulled through to my application it shows an apostrophe correctly. However, when I get a bigger set of data through the web service, it seems to improperly "decode" the value such as "Photo's" as "Photo???s"...
The exact same data is shown in Wireshark though, which leads me to believe that C# somehow incorrectly converts the response?
I removed the DebuggerStepThroughAttribute from Reference.cs to debug the actual web service code and I placed a breakpoint on the method that I invoke to get the return data, and adding the Output object to the QuickWatch also shows the data containing question marks instead of the correct data as well.
I can copy and paste the raw XML directly into an XML editor and the data shows correctly.
The method that I invoke returns an array of a Complex type. (see below) Could this be the cause of it?
The "Complex Type" array that I get out only has four fields and is basically defined as follows:
Code:
public partial class QueryResults
{
private string rowNumberField;
private string fieldNameField;
private string fieldValueField;
private string fieldFormattedValueField;
...
...
...
}
Unfortunately I can't control how the XML is encoded from the "host" Web Service, as this is "black box" functionality in our Host Application.
Any help anyone can offer will be greatly appreciated! :thumb:
Re: Strange Characters from Web Service Output - Data Dependent...
Did a bit more scratching:
I copied the input data from Wireshark into an XML file and invoked the same url using HttpWebRequest. When I "manually" invoke the web service, the return data is shown as expected. The problem only occurs when using the C# generated classes.
Any ideas?
Here's what I did to test:
Code:
public Service_Outputs QueryTest(Service_Inputs inputs)
{
Service_Outputs ret = new Service_Outputs();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(cGlobals.WSUrl);
req.Headers.Add("SOAPAction: \"" + soapAction + "\"");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
Stream stm = req.GetRequestStream();
XmlDocument doc = new XmlDocument();
doc.InnerXml = CreateQueryDoc(inputs); // Method will load text file
doc.Save(stm);
stm.Close();
WebResponse resp = null;
try
{
resp = req.GetResponse();
}
catch (WebException e)
{
WebResponse ex = e.Response;
HttpWebResponse http = (HttpWebResponse)ex;
string totalMsg = http.StatusCode.ToString() + "\n";
for (int i = 0; i < http.Headers.Count; i++)
totalMsg += http.Headers.GetKey(i) + " : " + http.Headers.Get(i) + "\n";
}
if (resp != null)
{
stm = resp.GetResponseStream();
StreamReader r = new StreamReader(stm);
string total = r.ReadToEnd();
ret = DeserializeObjectFromXML(total);
}
return ret;
}
As mentioned, the "ret" object holds the correct data.
Re: Strange Characters from Web Service Output - Data Dependent...
Could it be that the encoding isn't correct (maybe UTF-16 in stead of UTF-8)...
Re: Strange Characters from Web Service Output - Data Dependent...
I... have no idea. :) I checked the response and it's sending UTF-8 as a header. How can I confirm that the content received has the same encoding as specified in the header?
Re: Strange Characters from Web Service Output - Data Dependent...
You could put the resonse in notepad++ that checks the encoding automatically