好的,我又来了。我需要从我正在尝试开发的C#客户端使用Web服务。我正在使用HttpClient(来自.NET 4.5)但我正在使用 .NET 4 ,我从NuGet获得了这些Http包。除了两件事之外,我对WS一无所知:它做了什么(接收一个人的国家身份证号码并以xml格式返回他/她的一些信息),以及服务器要求使用它的凭证。
所以我的功能看起来像这样:
protected void rutBTN_Click(object sender, EventArgs e) //This void 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:pass123"); //Load credentials into a byte array (obviously censored since that's where the actual credentials go
client.BaseAddress = new Uri("http://wschsol.mideplan.cl"); //Base address of the web-service
var par = "mod_perl/xml/fps-by-rut?rut=" + rutTB.Text; //Creating a variable that holds the rest of the WS's URL
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(par).ContinueWith( //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
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
requestTask.Result.Content.ReadAsStreamAsync().ContinueWith(
(streamTask) =>
{
xmlResp.Load(streamTask.Result); //Loading the result stream into an XmlDocument object
xmlResp.GetEnumerator(); //Not really sure if this is needed.
XmlNodeList xmlLst = xmlResp.GetElementsByTagName("dv"); //Loading a list of xml nodes with the elements from the xml object of the type "dv"
});
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex.InnerException);
}
});
testLBL.Text = "Got here"; //This is to know if the execution reached this point, and it did.
testLBL.Visible = true;
dvLBL.Text = xmlLst.Item(0).InnerText; //HERE is the problem. VS tells me that the "xmlLst" name doesn't exist on the current context
dvLBL.Visible = true; //This label was meant to show the value of the 0 indexed item from the list.
}
else
{
testLBL.Text = "You must enter an RUT number"; // Error label
testLBL.Visible = true;
}
}
问题是,当我尝试从xmldocument对象获取数据时,它就像它不存在;或者更确切地说,只有当执行进入streamTask时才存在。我不知道如何处理这个问题。到现在为止,我只想从web服务的响应中获得ONE标签中的一个值,以便显示在我在文本框下的aspx页面上的标签上,只是为了知道我走的是正确的方式,然后才进入xml解析东西;但它不起作用。
我还尝试放置最后4条指令(那些为标签赋值并使它们可见的指令)在streamTask中,当我这样做时,它们检索数据并将其分配给元素,但我可以只有在使用断点进行调试时才会看到它,它不会显示在浏览器的页面上。
请您提前感谢你们给予我的帮助。
P.S。:以防它可能有用,这是我的故事(关于同一主题的问题之前的问题)Can't assign an http response content into an xml document on C#。
答案 0 :(得分:0)
将XmlNodeList xmlLst
声明移到任务之外。 client.GetAsync
对它自己的范围进行操作,因此当你尝试访问外部的xmlLst时会得到异常。所以你新代码应该是这样的:
HttpClient client = new HttpClient();
XmlNodeList xmlLst;
然后在GetAsync()
xmlLst = xmlResp.GetElementsByTagName("dv");
这应该这样做。