我使用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'不包含'GetResponse'的定义,并且没有扩展方法'GetResponse'接受类型'System.Net.Http.HttpResponseMessage'的第一个参数可以找到(你错过了吗?) using指令或程序集引用?)
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;
}
}
}
}
答案 0 :(得分:0)
您能否请添加此行并尝试:
这一行之后:
string resultContent = result.Content.ReadAsStringAsync().Result;
添加:
XElement incomingXml = XElement.Parse(resultContent);
或者您也可以使用ReadAsStreamAsync尝试此选项以将流读入XmlDocument:
string resultContent = result.Content.ReadAsStreamAsync().Result;
XDocument incomingXml = XDocument.Load(resultContent);