【发布时间】:2015-06-25 14:46:58
【问题描述】:
我正在开发 ac# 控制台应用程序,我正在使用 xml 文件将 Http Post request 制作为 Web api,我对 XML 和 Web 服务有点陌生,但我想出了以下代码请求但未能将xml data 传递给方法
static void Main(string[] args)
{
string desturl=@"https://xyz.abcgroup.com/abcapi/";
Program p = new Program();
System.Console.WriteLine(p.WebRequestPostData(desturl, @"C:\Applications\TestService\FixmlSub.xml"));
}
public string WebRequestPostData(string url, string postData)
{
System.Net.WebRequest req = System.Net.WebRequest.Create(url);
req.ContentType = "text/xml";
req.Method = "POST";
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postData);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
using (System.Net.WebResponse resp = req.GetResponse())
{
if (resp == null) return null;
using (System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream()))
{
return sr.ReadToEnd().Trim();
}
}
}
由于显而易见的原因,上面的代码抛出 404 错误,因为我认为我没有正确传递 xml data
我可以知道如何解决这个问题吗?
【问题讨论】:
-
404 表示找不到 URL。您需要激活一些身份验证机制才能从 System.Net.WebRequest 中检索 https:// 内容,例如参见 stackoverflow.com/questions/560804/…。
-
那么,我是否正确传递了 xml 数据?
标签: c# xml web-services http-post