无法在C#上将http响应内容分配到xml文档中 [英] Can't assign an http response content into an xml document on C#

查看:55
本文介绍了无法在C#上将http响应内容分配到xml文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我需要使用这种网络服务,对此我一无所知,我所知道的只是要做什么:它会接收一个人的身份证号码,并以xml格式返回有关他的一些信息。我需要在aspx页面上的C#客户端上使用它。

Ok there is this webservice that I need to consume from which I know nothing about, all I know is what it's meant to do: it receives a person's national ID number and returns some information about him on xml format. I need to consume it from a C# client on an aspx page.

由于我无法创建WS的Web引用,因此来到SO并首先问了这个问题:
未知的WebService描述,并从C#中使用它

Since I couldn't create a web reference to the WS, I came to SO and asked this first: Unknown WebService description and consume it from C#

在这个问题上,我被建议改为使用HttpClient而不是引用,因此我开始四处搜索并学到了一些东西。现在我已经能够做到这一点:

On that question I was suggested to rather make an HttpClient instead of a reference, so I started googling around and learned some stuff; right now I've been able to do this:

protected void rutBTN_Click(object sender, EventArgs e) //This function triggers when the user clicks the button 
    {                                                   //aside the textbox where they're meant to input the ID number
        if (rutTB.Text != "")   //rutTB is the textbox aside the button
        {
            HttpClient client = new HttpClient(); // Here i'm creating an instance of an HTTP client
            var byteArray = Encoding.ASCII.GetBytes("user:password123"); //Load credentials into a byte array (censored since there should be the actual credentials)
            client.BaseAddress = new Uri("http://wschsol.mideplan.cl"); //Base address of the web-service
            var hResp = "mod_perl/xml/fps_by_rut?rut=" + rutTB.Text; //Creating a variable that holds the rest of the WS's URL with the ID number value added to it
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); //Server authentication header with the byte array turned into a string
            client.GetAsync(hResp).ContinueWith( //Here I'm sending the GET request to the WS
                (requestTask) =>
                    {
                        HttpResponseMessage resp = requestTask.Result;    //This task receives the WS's http response and assigns it to a HttpResponseMessage variable called resp
                        try
                        {
                            resp.EnsureSuccessStatusCode(); //This line is to check that the response was successful or else throw an exception
                            XmlDocument xmlResp = new XmlDocument(); //Creating an instance of an xml document
                            resp.Content.ReadAsStringAsync<XmlDocument>().ContinueWith(  //OK HERE IS WHERE I'M LOST AT, trying to take the content of the http response into my xml document instance
                            (readTask) =>
                            {
                                /* This is where i want to parse the xml document data obtained into some grids or something. */
                            });
                        }
                        catch (Exception ex)
                        {
                            throw new Exception(ex.Message, ex.InnerException);
                        }
                    });

        }
        else
        {
            testLBL.Text = "You must enter an RUT number"; // Error label
            testLBL.Visible = true;
        }
    }

我迷失的代码行是我的VS2010强调了这一点。我知道这行是错误的,我基于此代码段 https:// code .msdn.microsoft.com / introduction-to-httpclient-4a2d9cee ;那里的人将响应加载到JsonArray而不是XML文档上。因此,如果你们能告诉我如何正确设置代码行,以及我的功能是否有其他错误,我将非常感谢。无论如何,Web服务的响应是通过浏览器以XML格式发出的,所以也许我什至不需要将其转换为xml文档?我很迷茫,我在软件开发领域还算是新手,所以如果你们能帮助我,我将非常感激。

The code line on which I'm lost at is underlined by my VS2010. I know this line is wrong, I based myself on this snippet https://code.msdn.microsoft.com/introduction-to-httpclient-4a2d9cee; the guy there loads the response onto a JsonArray instead of an XML Document. So i'd be greatly thankful if you guys could tell me how to get that code line right, and if something else is wrong in my function. Anyways, the webservice's response comes through the browser on XML format, so maybe I don't even need to convert it to an xml document?? I'm pretty lost, i'm quite a noob yet at software development so i'll be hugely grateful if you guys can help me out.

谢谢你们

推荐答案

尝试一下:

XmlDocument doc;
t.Result.Content.ReadAsStreamAsync().ContinueWith(
    (streamTask) =>
    {
        doc = new XmlDocument();
        doc.Load(streamTask.Result);
     });

然后,毕业至此:

var xmlStream = await t.Result.Content.ReadAsStreamAsync();
doc = new XmlDocument();
doc.Load(xmlStream);

在.NET 4.5中,尽可能使用 await而不是 ContinueWith。

In .NET 4.5, use "await" instead of "ContinueWith" when possible.

这篇关于无法在C#上将http响应内容分配到xml文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆