C# Can';t将http响应内容分配到C上的xml文档中#

C# Can';t将http响应内容分配到C上的xml文档中#,c#,xml,web-services,dotnet-httpclient,C#,Xml,Web Services,Dotnet Httpclient,好的,我需要使用这个Web服务,我对它一无所知,我只知道它的用途:它接收一个人的国家ID号,并以xml格式返回一些关于他的信息。我需要从aspx页面上的C#客户端使用它 因为我无法创建对WS的web引用,所以我先问了这么一个问题: 在这个问题上,有人建议我做一个HttpClient而不是一个推荐人,所以我开始在谷歌上搜索,并学到了一些东西;现在我已经能够做到这一点: protected void rutBTN_Click(object sender, EventArgs e) //This f

好的,我需要使用这个Web服务,我对它一无所知,我只知道它的用途:它接收一个人的国家ID号,并以xml格式返回一些关于他的信息。我需要从aspx页面上的C#客户端使用它

因为我无法创建对WS的web引用,所以我先问了这么一个问题:

在这个问题上,有人建议我做一个HttpClient而不是一个推荐人,所以我开始在谷歌上搜索,并学到了一些东西;现在我已经能够做到这一点:

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;
        }
    }
protectedvoid rutBTN\u Click(object sender,EventArgs e)//当用户单击按钮时,此函数将触发
{//将文本框放在一边,在那里输入ID号
if(rutTB.Text!=“”)//rutTB是按钮旁边的文本框
{
HttpClient client=new HttpClient();//这里我正在创建一个HTTP客户端的实例
var byteArray=Encoding.ASCII.GetBytes(“用户:password123”);//将凭据加载到字节数组中(由于应该存在实际凭据,因此会进行删除)
client.BaseAddress=新Uri(“http://wschsol.mideplan.cl“”;//web服务的基址
var hResp=“mod_perl/xml/fps_by_rut?rut=“+rutTB.Text;//创建一个变量,该变量保存WS-URL的其余部分,并向其添加ID号值
client.DefaultRequestHeaders.Authorization=new System.Net.Http.Headers.AuthenticationHeaderValue(“Basic”,Convert.ToBase64String(byteArray));//将字节数组转换为字符串的服务器身份验证头
client.GetAsync(hResp).ContinueWith(//这里我将GET请求发送到WS
(请求任务)=>
{
HttpResponseMessage resp=requestTask.Result;//此任务接收WS的http响应并将其分配给名为resp的HttpResponseMessage变量
尝试
{
resp.ensureccessStatusCode();//此行用于检查响应是否成功,或者引发异常
XmlDocument xmresp=new XmlDocument();//创建xml文档的实例
resp.Content.ReadAsStringAsync().ContinueWith(//OK这里是我迷路的地方,试图将http响应的内容放入我的xml文档实例中
(readTask)=>
{
/*这就是我想要将获得的xml文档数据解析到一些网格或其他地方的地方*/
});
}
捕获(例外情况除外)
{
抛出新异常(例如Message,例如InnerException);
}
});
}
其他的
{
testLBL.Text=“您必须输入车辙号”;//错误标签
testLBL.Visible=true;
}
}
我丢失的代码行在VS2010中加了下划线。我知道这句话是错的,我是根据这段代码写的;那里的家伙将响应加载到JsonArray而不是XML文档中。所以,如果你们能告诉我如何使代码行正确,如果我的函数中有其他错误,我将非常感激。无论如何,Web服务的响应是通过浏览器以XML格式提供的,所以我甚至不需要将其转换为XML文档??我很迷路,我在软件开发方面还不是个好学生,所以如果你们能帮我的话,我将非常感激

谢谢你们的帮助。

试试这个:

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中,尽可能使用“wait”而不是“ContinueWith”。

p.S.我曾尝试使用ReadAsAsync(),但似乎不起作用。这可能会更简单。显然这是可行的,但我想知道(在开始解析工作之前),当调用Web服务时,我的xmlDocument是否正确地接收到与xml在浏览器上显示的数据相同的数据。。。我该怎么做??我在stream任务和xml load指令上设置了一些断点,但当我检查XmlDocument对象时,似乎找不到任何断点。有什么想法吗?然后用ReadAsStringAsync()替换ReadAsStreamAsync()。这样可以很容易地在其中放置断点并查看字符串,或将其写入控制台。谢谢,我可以将其作为流进行检查,但出于某种原因,我的xmlDocument将响应放入名为“InnerXml”的属性中;就在那里(doc.InnerXml)是整个xml响应,就好像它只是一个长字符串,我没有child、name或tags;所有其他属性都是空的或null,也没有IEnumerable…我做错了什么?这就是InnerXml。它是整个xml作为字符串。它将是从HTTP请求中获得的原始字符串,包括ca中的任何子标记等InnerXml是否与原始流中的内容有所不同?