我先拼出了一个soap头,然后写入http流。代码如下:
HttpWebRequest httpRequest;
//以指定的URL创建请求对象
httpRequest = (HttpWebRequest)WebRequest.Create(url);
//构造HTTP POST 消息,并把SOAP消息放到它的消息主体中
try
{
//根据用户的输入设置请求对象的标题字段
httpRequest.ContentType = "text/xml; charset=utf-8";
//添加SOAP Action 字段
string action = "http://www.semc.gov.cn/";
httpRequest.Headers.Add("SOAPAction", action + "/GetCountyHourlyDataFromNow");
httpRequest.KeepAlive = false;
//设置请求方法
httpRequest.Method = "POST";
string data = this.BuildSoap("GetCountyHourlyDataFromNow", null, null, url, "http://www.semc.gov.cn/");
//根据用户的输入构造SOAP封装,并把它保存到HTTP请求消息的流中
byte[] req_soap = Encoding.UTF8.GetBytes(data);
//设置HTTP请求消息的ContentLength字段
httpRequest.ContentLength = req_soap.GetLength(0);
Stream rs = httpRequest.GetRequestStream();
rs.Write(req_soap, 0, (int)(httpRequest.ContentLength));
rs.Close();
//获取请求返回的资源
var webResponse = httpRequest.GetResponse();
using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
{
string result = "";
return result = myStreamReader.ReadToEnd();
}
}
catch (Exception ex)
{
return ex.Message;
}
代码在上面,var webResponse = httpRequest.GetResponse();这句提示
远程服务器返回错误: (500) 内部服务器错误。