gpt4 book ai didi

c# - 从 PostAsync() 读取 XML 响应

转载 作者:数据小太阳 更新时间:2023-10-29 02:44:20 25 4
gpt4 key购买 nike

我正在使用 PostAsync()发送数据,代码完全按预期执行。我将数据发送到的站点提供了一个 XML通知成功或失败的响应通知。 XML响应是这样的:成功 的帖子返回 1

<?xml version="1.0" encoding="utf-8" ?>
<result>
<success>1</success>
<uploadid/>
</errors>
</result>

虽然不成功 返回 <error> 之间的原因标签

<?xml version="1.0" encoding="utf-8" ?>
<result>
<success>0</success>
<iploadid/>
<errors>
<error>File is not in correct format</error>
</errors>
</result>

这是我用来执行 PostAsync()C# 语法我需要添加什么才能阅读 XML页面的响应?

private void SendAsyncData()
{
string connectionString = "Data Source=hmod;Initial Catalog=salesdb;uid=userid;password=password;MultipleActiveResultSets=True";
DataTable grds = new DataTable();
using (var con = new SqlConnection(connectionString))
using (var cmd = new SqlCommand("procedureone", con))
using (var da = new SqlDataAdapter(cmd))
{
cmd.CommandType = CommandType.StoredProcedure;
da.Fill(grds);
}
var client = new HttpClient();
var q = string.Empty;
foreach (DataRow row in grds.Rows)
{
var parameterSet = new List<KeyValuePair<string, string>>();
for (int i = 0; i < grds.Columns.Count; ++i)
{
parameterSet.Add(new KeyValuePair<string, string>(grds.Columns[i].ColumnName, row[i].ToString()));

}

var result = client.PostAsync("http://weneedsampledata/ashx", new FormUrlEncodedContent(parameterSet)).Result;
string resultContent = result.Content.ReadAsStringAsync().Result;
}
}

编辑
目前,即使接收数据的站点抛出错误,此语法也会返回代码 200(成功),我需要一种“读取”XML 响应的方法来确定数据是否真的成功。我试图在下面添加以读取响应,但它给我一个编译错误:

'System.Net.Http.HttpResponseMessage' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'System.Net.Http.HttpResponseMessage' could be found (are you missing a using directive or an assembly reference?)

var response = result.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
var postresult = readStream.ReadToEnd();
var xml = System.Xml.Linq.XElement.Parse(postresult);
if (xml.Elements("success").FirstOrDefault().Value == "1")
{
lblgoodbad.Text = "It was good.";
}
else
{
var errors = xml.Elements("errors");
foreach (var error in errors.Elements("error"))
{
lblgoodbad.Text = error.Value;
}
}

编辑#2
感谢@Hussey 的帮助——我只是对我的语法做了一个快速调整,它就可以正常运行了。我的完整语法是这样的:

    private void SendAsyncData()
{
string connectionString = "Data Source=hmod;Initial Catalog=salesdb;uid=userid;password=password;MultipleActiveResultSets=True";
DataTable grds = new DataTable();
using (var con = new SqlConnection(connectionString))
using (var cmd = new SqlCommand("procedureone", con))
using (var da = new SqlDataAdapter(cmd))
{
cmd.CommandType = CommandType.StoredProcedure;
da.Fill(grds);
}
var client = new HttpClient();
var q = string.Empty;
foreach (DataRow row in grds.Rows)
{
var parameterSet = new List<KeyValuePair<string, string>>();
for (int i = 0; i < grds.Columns.Count; ++i)
{
parameterSet.Add(new KeyValuePair<string, string>(grds.Columns[i].ColumnName, row[i].ToString()));

}

var result = client.PostAsync("http://weneedsampledata/ashx", new FormUrlEncodedContent(parameterSet)).Result;
string resultContent = result.Content.ReadAsStringAsync().Result;
var xml = System.Xml.Linq.XElement.Parse(resultContent);
if (xml.Elements("success").FirstOrDefault().Value == "1")
{
lblgoodbad.Text = "It was good.";
}
else
{
var errors = xml.Elements("errors");
foreach (var error in errors.Elements("error"))
{
lblgoodbad.Text = error.Value;
}
}
}
}

最佳答案

能否请您添加这一行并尝试:

在这一行之后:

string resultContent = await result.Content.ReadAsStringAsync();

添加这个:

XElement incomingXml = XElement.Parse(resultContent);

或者您也可以使用 ReadAsStreamAsync 尝试此选项以将流读入 XmlDocument:

string resultContent = await result.Content.ReadAsStreamAsync();

XDocument incomingXml = XDocument.Load(resultContent);

关于c# - 从 PostAsync() 读取 XML 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36274570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com