我进入了未知领域 - 我必须第一次创建一个 httppost (xml)。我看过很多例子,但坦率地说,这对菜鸟来说有点混乱。
我目前正在处理的功能是:
public string SubmitRequest(string postUrl, string contentType, string postValues)
{
var req = WebRequest.Create(postUrl);
req.Method = "POST";
req.ContentType = contentType;
try
{
using (var reqStream = req.GetRequestStream())
using (var writer = new StreamWriter(reqStream))
{
writer.WriteLine(postValues);
}
var resp = req.GetResponse();
using (var respStream = resp.GetResponseStream())
using (var reader = new StreamReader(respStream))
{
return reader.ReadToEnd().Trim();
}
}
catch(WebException ex)
{
// do something here
}
return string.Empty;
}
我想我有2个问题:
就正确处理/关闭对象而言,代码是否正确?鉴于我使用的是 asp.net 4.0,这是编写 httppost 的最有效方式吗?
提前致谢