public void test()
{
StringBuilder soap = new StringBuilder();
soap.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
soap.Append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
soap.Append("<soap:Header>");
soap.Append("<MySoapHeader xmlns=\"http://www.test.com/\" soap:mustUnderstand=\"1\">");
soap.Append("<Username>admin</Username>");
soap.Append("<Password>admin123</Password>");
soap.Append("</MySoapHeader>");
soap.Append("</soap:Header>");
soap.Append("<soap:Body>");
soap.Append("<getSecuritiesTreeByRemote xmlns=\"http://www.test.com/\">");
soap.Append("</getSecuritiesTreeByRemote>");
soap.Append("</soap:Body>");
soap.Append("</soap:Envelope>");
//Uri uri = new Uri("http://www.webxml.com.cn/WebServices/WeatherWS.asmx");
Uri uri = new Uri(strURL);
WebRequest webRequest = WebRequest.Create(uri);
webRequest.ContentType = "text/xml; charset=utf-8";
webRequest.Method = "POST";
using (Stream requestStream = webRequest.GetRequestStream())
{
byte[] paramBytes = Encoding.UTF8.GetBytes(soap.ToString());
requestStream.Write(paramBytes, 0, paramBytes.Length);
}
//响应
WebResponse webResponse = webRequest.GetResponse();
using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
{
Console.WriteLine(myStreamReader.ReadToEnd());
} Console.ReadKey()
}
我用了上面的代码调用webservice,但现在服务器端加了验证,需要在soapheader里增加用户名和密码的信息才能访问到方法,我试了如上代码,但报500错误,不知为什么:(
{
StringBuilder soap = new StringBuilder();
soap.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
soap.Append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
soap.Append("<soap:Header>");
soap.Append("<MySoapHeader xmlns=\"http://www.test.com/\" soap:mustUnderstand=\"1\">");
soap.Append("<Username>admin</Username>");
soap.Append("<Password>admin123</Password>");
soap.Append("</MySoapHeader>");
soap.Append("</soap:Header>");
soap.Append("<soap:Body>");
soap.Append("<getSecuritiesTreeByRemote xmlns=\"http://www.test.com/\">");
soap.Append("</getSecuritiesTreeByRemote>");
soap.Append("</soap:Body>");
soap.Append("</soap:Envelope>");
//Uri uri = new Uri("http://www.webxml.com.cn/WebServices/WeatherWS.asmx");
Uri uri = new Uri(strURL);
WebRequest webRequest = WebRequest.Create(uri);
webRequest.ContentType = "text/xml; charset=utf-8";
webRequest.Method = "POST";
using (Stream requestStream = webRequest.GetRequestStream())
{
byte[] paramBytes = Encoding.UTF8.GetBytes(soap.ToString());
requestStream.Write(paramBytes, 0, paramBytes.Length);
}
//响应
WebResponse webResponse = webRequest.GetResponse();
using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
{
Console.WriteLine(myStreamReader.ReadToEnd());
} Console.ReadKey()
}
我用了上面的代码调用webservice,但现在服务器端加了验证,需要在soapheader里增加用户名和密码的信息才能访问到方法,我试了如上代码,但报500错误,不知为什么:(
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货