美文网首页
WebServices与WebRequest

WebServices与WebRequest

作者: 蓝色Hippie | 来源:发表于2018-02-02 14:05 被阅读15次

一.WebServices
1.1发布一个服务
新建Web工程,编写与发布,部署在IIS上,精简的样例:
https://www.cnblogs.com/Brambling/p/6815565.html
1.2服务的调用
WebServices的调用,只需在开发工具中配置服务,后再代码中调用传入约定好的报文即可。
二.WebRequest
string token=GetPostData(url,postString,"application/json");
//通过WebRequest请求数据
public string GetPostData(string url,string postString,string ContentType)
{
string result=string.Empty;
try
{
System.Net.ServicePointManager.ServerCertificateValidationCallback=new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertificatePolicy.RemoteCertificateValidationCallback);
byte[] postDate=Encoding.UTF8.GetBytes(postString);
HttpWebRequest request=(HttpWebRequest)WebRequest.Create(new Uri(url));
request.Method="Post":
request.ServicePoint.Expect100Continue=false;
request.ContentType=ContentType;
request.ContentLength=postData.Length;
using(Stream requestStream=request.GetRequestStream())
{
requestStream.Write(postData,0,postData.Length);
}
HttpWebResponse response=(HttpWebResponse)request.GetResponse();
StreamReader stream=new StreamReader(response.GetResponseStream());
result=stream.ReadToEnd();
stream.Close();
request.Abort();
response.Close();
}
catch(Exception ex)
{

}
return result;
}

internal class AcceptAllCertificatePolicy:ICertificatePolicy
{
public static bool RemoteCertificateValidationCallback(object sender,X509Certificate certificate,X509Chain chain,System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
}
public bool CheckValidationResult(ServicePoint srvPoint,System.Security.Cryptography.X509Certificate certificate,WebRequest request,int certificateProblem)
{
throw new NotImplementedException();
}
}

相关文章

网友评论

      本文标题:WebServices与WebRequest

      本文链接:https://www.haomeiwen.com/subject/aitjzxtx.html