简体   繁体   中英

Httpwebrequest give response removing all the XML formatting/tags

I have a .NET project which posts data to foreign web server in the form of a SOAP object via HTTP POST. This is done using a HttpWebRequest object. I get a response from the web server, which I am capturing with an HttpWebResponse object. This response object is also XML surrounded by a SOAP envelope.

The problem is, when I take the response and output it to the screen with ToString it apparently nukes all of the tags and just combines it all into a single string.

How can I output the returned XML from the web server without removing all the XML formatting/tags?

Here is the code I am using:

public static XmlDocument PostXMLTransaction(string v_strURL, XmlDocument v_objXMLDoc, string SoapAction, string username, string password)
    {
XmlDocument XMLResponse = null;
HttpWebRequest objHttpWebRequest;
HttpWebResponse objHttpWebResponse = null;

Stream objRequestStream = null;
Stream objResponseStream = null;

XmlTextReader objXMLReader;
objHttpWebRequest = (HttpWebRequest)WebRequest.Create(v_strURL);

try
{
    byte[] bytes;
    bytes = System.Text.Encoding.ASCII.GetBytes(v_objXMLDoc.InnerXml);
    objHttpWebRequest.Method = "POST";
    objHttpWebRequest.ContentLength = bytes.Length;
    objHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";
    objHttpWebRequest.Headers.Add("SOAPAction", "Some URL/"+SoapAction);

    NetworkCredential nc = new NetworkCredential(username, password);

    objHttpWebRequest.Credentials = nc;

    objRequestStream = objHttpWebRequest.GetRequestStream();
    objRequestStream.Write(bytes, 0, bytes.Length);
    objRequestStream.Close();

    objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();

    if (objHttpWebResponse.StatusCode == HttpStatusCode.OK)
    {
        objResponseStream = objHttpWebResponse.GetResponseStream();
        objXMLReader = new XmlTextReader(objResponseStream);

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(objXMLReader);
        XMLResponse = xmldoc;
        objXMLReader.Close();
    }
        objHttpWebResponse.Close();
        }
        catch (Exception we)
        {
            //TODO: Add custom exception handling
            throw new Exception(we.Message);
        }

        finally
        {
            //Close connections
            objRequestStream.Close();
            objResponseStream.Close();
            objHttpWebResponse.Close();

            //Release objects
            objXMLReader = null;
            objRequestStream = null;
            objResponseStream = null;
            objHttpWebResponse = null;
            objHttpWebRequest = null;
        }

        //Return
        return XMLResponse;
    }

I am getting response like below. Soap envelop only have xml tags other letter come with different characters.

 <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns1:getHotelDetail xsi:type="xsd:string" xmlns:ns1="http://axis.frontend.hydra.hotelbeds.com">&lt;HotelDetailRS xmlns="http://www.hotelbeds.com/schemas/2005/06/messages" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.hotelbeds.com/schemas/2005/06/messages HotelDetailRS.xsd" echoToken="DummyEchoToken"&gt;&lt;AuditData&gt;&lt;ProcessTime&gt;570&lt;/ProcessTime&gt;&lt;Timestamp&gt;2015-03-20 07:50:07.272&lt;/Timestamp&gt;&lt;RequestHost&gt;54.169.51.224&lt;/RequestHost&gt;&lt;ServerName&gt;FORM&lt;/ServerName&gt;&lt;ServerId&gt;FO&lt;/ServerId&gt;&lt;SchemaRelease&gt;2005/06&lt;/SchemaRelease&gt;&lt;HydraCoreRelease&gt;2015.01.14&lt;/HydraCoreRelease&gt;&lt;HydraEnumerationsRelease&gt;N/A&lt;/HydraEnumerationsRelease&gt;&lt;MerlinRelease&gt;0&lt;/MerlinRelease&gt;&lt;/AuditData&gt;&lt;Hotel xsi:type="ProductHotel"&gt;&lt;Code&gt;52317&lt;/Code&gt;&lt;Name&gt;Hampton Inn &amp;amp; Suites Montreal&lt;/Name&gt;&lt;DescriptionList&gt;&lt;Description type="HotelDescription" languageCode="ENG" languageName="Ingles"&gt;where sophistication and culture meet...welcome to the Hampton Inn &amp;amp; Suites by Hilton Montreal (Dorval). At the Hampton Inn &amp;amp; Suites by Hilton&amp;#8482; hotel in Montreal (Dorval), we&amp;apos;re proud of our city. Caf&amp;#233;s, theatres and universities dot this international city, giving Montreal a blend of big-city sophistication and old-world charm. Montreal makes visitors comfortable but also engages them with plenty to do and see. In short, the city remains a delightful blend of old and new. Within just kilometers of the Hampton Inn &amp;amp; Suites by Hilton&amp;#8482; hotel in Montreal (Dorval), you&amp;apos;ll find lovely golf courses, restaurants and shopping. Plus, driving into downtown Montreal takes a mere 15 minutes. The Casino Botanical Gardens and old Montreal are near our hotel in Montreal (Dorval), too. So whether you&amp;apos;re in town to soak up some culture or perhaps see the many metropolitan charms of Montreal, you&amp;apos;ll soon discover that the Hampton Inn &amp;amp; Suites by Hilton hotel in Montreal (Dorval) is where sophistication and culture meet. services &amp;amp; amenities Here at the Hampton Inn &amp;amp; Suites by Hilton hotel in Montreal (Dorval), we&amp;apos;re passionate about taking good care of you. That&amp;apos;s why we offer a broad range of services and amenities to make your stay exceptional. Whether you&amp;apos;re planning a corporate meeting or need accommodations for a family reunion or your child&amp;apos;s sporting group, we&amp;apos;re delighted to offer you easy planning and booking tools to make the process quick and organized.* Meetings &amp;amp; Events * Local Restaurant Guide&lt;/Description&gt;&lt;/DescriptionList&gt;&lt;ImageList&gt;&lt;Image&gt;&lt;Type&gt;GEN&lt;/Type&gt;&lt;Order&gt;8&lt;/Order&gt;&lt;Description&gt;General view&lt;/Description&gt;&lt;Url&gt;http://www.hotelbeds.com/giata/05/052317/052317a_hb_a_008.jpg&lt;/Url&gt;&lt;/Image&gt;&lt;Image&gt;&lt;Type&gt;GEN&lt;/Type&gt;&lt;Order&gt;9&lt;/Order&gt;&lt;Description&gt;General view&lt;/Description&gt;&lt;Url&gt;http://www.hotelbeds.com/giata/05/052317/052317a_hb_a_009.jpg&lt;/Url&gt;&lt;/Image&gt;&lt;Image&gt;&lt;Type&gt;GEN&lt;/Type&gt;&lt;Order&gt;10&lt;/Order&gt;&lt;Description&gt;General view&lt;/Description&gt;&lt;Url&gt;http://www.hotelbeds.com/giata/05/052317/052317a_hb_a_010.jpg&lt;/Url&gt;&lt;/Image&gt;&lt;Image&gt;&lt;Type&gt;GEN&lt;/Type&gt;&lt;Order&gt;11&lt;

The output is correct, your response is actually a string which in turn is xml inside. getHotelDetail response is a valid string object. You may have to create another XmlDocument from getHotelDetail string content.

I believe this is something internal to Web Service where the response is Xml DataSet instead of XmlDocument.

However, why don't you simply import Web Services from Add Service Reference instead of manually fetching Web Service Response, .NET has full support for automatically fetching and marshaling response to proper objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM