How to call webservices with ASP.NET at Run time?
1. 一般情况下,我们都用HttpWebRequest来调用WebServices.
建立WebServices:
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Runtime.Serialization;
/// <summary>
/// Summary description for CustomerWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
[ScriptService]
public class CustomerWebService : System.Web.Services.WebService
{
public CustomerWebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
//[WebMethod]
//public string HelloWorld() {
// return "Hello World";
//}
[ScriptMethod]
[WebMethod]
public string Register(long id, string data1)
{
return "ID.CUSTOMER";
}
[ScriptMethod]
[WebMethod]
public string HelloWorld(Person p)
{
return p.Name;
}
}
[DataContract]
public class Person
{
[DataMember]
public string Name { get; set; }
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Runtime.Serialization;
/// <summary>
/// Summary description for CustomerWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
[ScriptService]
public class CustomerWebService : System.Web.Services.WebService
{
public CustomerWebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
//[WebMethod]
//public string HelloWorld() {
// return "Hello World";
//}
[ScriptMethod]
[WebMethod]
public string Register(long id, string data1)
{
return "ID.CUSTOMER";
}
[ScriptMethod]
[WebMethod]
public string HelloWorld(Person p)
{
return p.Name;
}
}
[DataContract]
public class Person
{
[DataMember]
public string Name { get; set; }
}
在IE中输入webservices地址(http://localhost:8485/MySite/CustomerWebService.asmx?op=HelloWorld),可看到调用该services的soap消息格式.
代码
POST /MySite/CustomerWebService.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/HelloWorld"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloWorld xmlns="http://tempuri.org/">
<p>
<Name>string</Name>
</p>
</HelloWorld>
</soap:Body>
</soap:Envelope>
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/HelloWorld"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloWorld xmlns="http://tempuri.org/">
<p>
<Name>string</Name>
</p>
</HelloWorld>
</soap:Body>
</soap:Envelope>
2.调用webServices
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(OnValidationCallback);
try
{
//string param = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
StringBuilder strb = new StringBuilder();
strb.Append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
strb.Append("<soap:Body>");
strb.Append("<HelloWorld xmlns=\"http://tempuri.org/\">");
strb.Append("<p>");
strb.Append("<Name>Personss</Name>");
strb.Append("</p>");
strb.Append("</HelloWorld>");
strb.Append("</soap:Body>");
strb.Append("</soap:Envelope>");
string param=strb.ToString();
byte[] bs = Encoding.UTF8.GetBytes(param);
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create(
"http://localhost:8485/MySite/CustomerWebService.asmx");
myRequest.Method = "POST";
myRequest.ContentType = "text/xml; charset=utf-8";
myRequest.Headers.Add("SOAPAction", "http://tempuri.org/HelloWorld");
myRequest.ContentLength = bs.Length;
using (Stream reqStream = myRequest.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())
{
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string responseString = sr.ReadToEnd();
Response.Write("结果:" + responseString);
}
}
catch (Exception ex)
{
Response.Write(System.DateTime.Now.ToShortTimeString()
+ "LBS EXCEPTION:" + ex.Message);
Response.Write(ex.StackTrace);
}
}
public bool OnValidationCallback(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(OnValidationCallback);
try
{
//string param = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
StringBuilder strb = new StringBuilder();
strb.Append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
strb.Append("<soap:Body>");
strb.Append("<HelloWorld xmlns=\"http://tempuri.org/\">");
strb.Append("<p>");
strb.Append("<Name>Personss</Name>");
strb.Append("</p>");
strb.Append("</HelloWorld>");
strb.Append("</soap:Body>");
strb.Append("</soap:Envelope>");
string param=strb.ToString();
byte[] bs = Encoding.UTF8.GetBytes(param);
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create(
"http://localhost:8485/MySite/CustomerWebService.asmx");
myRequest.Method = "POST";
myRequest.ContentType = "text/xml; charset=utf-8";
myRequest.Headers.Add("SOAPAction", "http://tempuri.org/HelloWorld");
myRequest.ContentLength = bs.Length;
using (Stream reqStream = myRequest.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())
{
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string responseString = sr.ReadToEnd();
Response.Write("结果:" + responseString);
}
}
catch (Exception ex)
{
Response.Write(System.DateTime.Now.ToShortTimeString()
+ "LBS EXCEPTION:" + ex.Message);
Response.Write(ex.StackTrace);
}
}
public bool OnValidationCallback(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
}
如果出现500内部服务器错误:
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(OnValidationCallback);
public bool OnValidationCallback(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
{
return true;
}