我相信您正在使用 UFT API 模块。UFT 解析 XML,因此您将无法在数据部分输入 XML 特殊字符 & 和 <。您可以使用以下函数来进行 xml 请求发布。这就是我现在在我的项目中使用的。
public static string HttpRequest(string url, string xml)
{
string response = string.Empty;
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Timeout = 10000;
try
{
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(xml);
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = bytes.Length;
httpWebRequest.ContentType = "text/xml; encoding='utf-8'";
using (Stream requestStream = httpWebRequest.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
}
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
if (httpWebResponse.StatusCode == HttpStatusCode.OK)
{
using (Stream responseStream = httpWebResponse.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
response = reader.ReadToEnd();
}
}
httpWebResponse.Close();
}
catch (Exception ex)
{
throw new Exception ("Error");
}
finally
{
if (httpWebResponse != null)
{
httpWebResponse.Close();
httpWebResponse = null;
}
httpWebRequest = null;
}
return response;
}