我相信你正在使用UFT API模塊。 UFT解析XML,因此您將無法在數據部分輸入XML特殊字符&和<。你可以使用下面的函數來做xml request post。這就是我現在使用的項目。
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;
}