Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Perl web service which is consumed by c# client is returning null, what could be the possible reason.
Perl web service seems returning data though, below is the response perl web service is generating with some exception

Exception: System.InvalidOperationException: Client found response con +tent type of '', but expected 'text/xml'. The request failed with the error message: -- Content-type : text/xml HTTP/1.1 200 OK Content-Length: 1449 Content-Type: text/xml; charset=utf-8 SOAPServer: SOAP::Lite/Perl/1.17 <?xml version="1.0" encoding="UTF-8"?><soap:Envelope soap:encodingStyl +e="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://sche +mas.xmlsoap .org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/en +coding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http +://www.w3.o rg/2001/XMLSchema-instance"><soap:Body><MRWebServices__searchResponse +xmlns="MRWebServices"><return xsi:type="xsd:base64Binary">PGRhdGE+CiA +8YXJyYXk+Ci AgPGl0ZW0ga2V5PSIwIj4KICAgPG1hcD4KICAgIDxpdGVtIGtleT0ibXJpZCI+MTwvaXRl +bT4KICAgIDxpdGVtIGtleT0ibXJ0aXRsZSI+VGVzdCB0aWNrZXQgZm9yIGVkaXQgaXNzd +WUuPC9pdGVt PgogICA8L21hcD4KICA8L2l0ZW0+CiAgPGl0ZW0ga2V5PSIxIj4KICAgPG1hcD4KICAgID +xpdGVtIGtleT0ibXJpZCI+MjwvaXRlbT4KICAgIDxpdGVtIGtleT0ibXJ0aXRsZSI+VGV +zdCB0aWNrZX QgZm9yIGVkaXQgaXNzdWUyLjwvaXRlbT4KICAgPC9tYXA+CiAgPC9pdGVtPgogIDxpdGVt +IGtleT0iMiI+CiAgIDxtYXA+CiAgICA8aXRlbSBrZXk9Im1yaWQiPjM8L2l0ZW0+CiAgI +CA8aXRlbSBr ZXk9Im1ydGl0bGUiPlRlc3QgdGlja2V0IGZvciBlZGl0IGlzc3VlMy48L2l0ZW0+CiAgID +wvbWFwPgogIDwvaXRlbT4KICA8aXRlbSBrZXk9IjMiPgogICA8bWFwPgogICAgPGl0ZW0 +ga2V5PSJtcm lkIj40PC9pdGVtPgogICAgPGl0ZW0ga2V5PSJtcnRpdGxlIj5UZXN0IHRpY2tldCBmb3Ig +ZWRpdCBpc3N1ZTQuPC9pdGVtPgogICA8L21hcD4KICA8L2l0ZW0+CiAgPGl0ZW0ga2V5P +SI0Ij4KICAg PG1hcD4KICAgIDxpdGVtIGtleT0ibXJpZCI+NTwvaXRlbT4KICAgIDxpdGVtIGtleT0ibX +J0aXRsZSI+VGVzdCB0aWNrZXQgZm9yIGVkaXQgaXNzdWU1LjwvaXRlbT4KICAgPC9tYXA ++CiAgPC9pdG VtPgogPC9hcnJheT4KPC9kYXRhPgo=</return></MRWebServices__searchResponse +></soap:Body></soap:Envelope> --. at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadRespons +e(SoapClientMessage message, WebResponse response, Stream responseStr +eam, Boolea n asyncCall) at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(Stri +ng methodName, Object[] parameters) at Search.MRWebServices__search(String usr, String pw, String extra +Info, String query, Int32 api) at main.Main()

Please advice, Thank you

Replies are listed 'Best First'.
Re: Perl web service consumed by c# client returns null.
by haj (Vicar) on Dec 12, 2019 at 16:38 UTC
    Isn't the error message quite clear? It looks as if the web service fails to provide a Content-Type header, but the client expects one. According to RFC7231, the web service SHOULD provide that header, and apparently the C# client doesn't want to guess. The message tells you that the client expects a content type of text/xml, which is fair enough.
      It looks as if the web service fails to provide a Content-Type header, but the client expects one.

      I'm not the original Anonymous Monk, but will respond: I would have thought the same thing, except that the error message is included in the message, and appears to be showing the content that the client received. That message shows not one but two Content-Type headers. If the error message isn't showing what was received from the service, but some other content, I would be confused as a user of that client.

      I'm wondering if seeing two Content-Type headers instead of one-and-only-one is confusing the client. Or maybe the headers don't have the expected newline sequence, so it shows up when printed as being on multiple lines, but the client either sees it as one line or sees extra characters at the start or end of each of the header lines.

      (edit first paragraph for clarity)

        Ah, indeed. Thanks for pointing that out!

        So the first header is Content-type (with a lowercase "t" in type), and it seems to come before the HTTP 1.1 line. This is an invalid HTTP response and might cause the client to abort parsing. If that's the case, then the error message isn't as good as it could be...

        That or the client does not see headers at all. Header items should be separated by \r\n . The end of headers is denoted by \r\n\r\n

Re: Perl web service consumed by c# client returns null.
by Anonymous Monk on Dec 13, 2019 at 08:09 UTC

    Hi, Monks,

    Please don't get confused with two headers. first header you see is typed by me in the perl webservice to see what web service is returning.
    Line from Http is what web service seems returning, but c# client getting null value.

    Exception: System.InvalidOperationException: Client found response con +tent type of '', but expected 'text/xml'. The request failed with the error message: -- Content-Type : text/xml #I have added this line in the perl web servic +e like print "\nContent-type : text/xml\n\n" to check what exactly it + returns. #################################################################### HTTP/1.1 200 OK Content-Length: 1468 Content-Type: text/xml; charset=utf-8 SOAPServer: SOAP::Lite/Perl/1.17 <?xml version="1.0" encoding="UTF-8"?><soap:Envelope soap:encodingStyl +e="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://sche +mas.xmlsoap .org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/en +coding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http +://www.w3.o rg/2001/XMLSchema-instance"><soap:Body><MRWebServices__searchResponse +xmlns="MRWebServices"><return xsi:type="xsd:string"><data> <array> <item key="0"> <map> <item key="mrid">1</item> <item key="mrtitle">Test ticket for edit issue.</item> </map> </item> <item key="1"> <map> <item key="mrid">2</item> <item key="mrtitle">Test ticket for edit issue2.</item> </map> </item> <item key="2"> <map> <item key="mrid">3</item> <item key="mrtitle">Test ticket for edit issue3.</item> </map> </item> <item key="3"> <map> <item key="mrid">4</item> <item key="mrtitle">Test ticket for edit issue4.</item> </map> </item> <item key="4"> <map> <item key="mrid">5</item> <item key="mrtitle">Test ticket for edit issue5.</item> </map> </item> </array> </data> </return></MRWebServices__searchResponse></soap:Body></soap:Envelope> --. at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadRespons +e(SoapClientMessage message, WebResponse response, Stream responseStr +eam, Boolea n asyncCall) at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(Stri +ng methodName, Object[] parameters) at Search.MRWebServices__search(String usr, String pw, String extra +Info, String query) at main.Main()

    Thank you.

      So now you got me really confused.

      The text beginning with Exception: System.InvalidOperationException: looks as if it is printed by the C# client, as Perl doesn't throw such an exception. The stack trace at the end also looks rather C#-ish to me. So who is composing the "error message" enclosed by the two -- strings, and from what input? You write I have added this line in the perl web service - but apparently the client embedded it into its "error message."

      The string printed by print "\nContent-type : text/xml\n\n" useless for debugging, it is just a constant. If you send it to the client, then it breaks the HTTP response. You should at least do something like that:

      print "HTTP/1.1 200 OK\n; print "Content-type : text/plain\n\n";

      ...because after inserting these lines, the content type of the response is no longer valid XML, but can still be read as text.

      You could, of course, also point a simple client like LWP's HEAD program to the service to check whether the response is well-formed.

        Hi Haj,
        when I added print "Content-type : text/xml\n\n" in perl webservice, after this there is no content-type error but I am getting below error

        Exception: System.InvalidOperationException: Response is not well-form +ed XML. ---> System.Xml.XmlException: Data at the root level is inval +id. Line 1, position 1. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlReader.MoveToContent() at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadRespons +e(SoapClientMessage message, WebResponse response, Stream responseStr +eam, Boolea n asyncCall) at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(Stri +ng methodName, Object[] parameters) --- End of inner exception stack trace --- at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(Stri +ng methodName, Object[] parameters) at Search.MRWebServices__search(String usr, String pw, String extra +Info, String query) at main.Main()

        Thank you

      Content-Type : text/xml #I have added this line in the perl web service like print "\nContent-type : text/xml\n\n" to check what exactly it returns.
      

      if you have access to the service code, it would be trivial to crack. For example, start by finding out why the error message complains that it found an empty Content-Type but you print text/xml and your client sends text/xml; charset=utf-8