C#写接口怎么分URL地址给别人用
1个回答
展开全部
参考
using System.Net;
using System.Security.Cryptography;
using System.IO;
using Newtonsoft.Json;
namespace CRM.BLL.SalesClue
{
public class AddSalesClueLogic
{
public void PushSaleClueData(string companyUserName,string mobile,string email,string post,string saleClueSource,string companyType)
{
//AppSecret
var AppSecret = "TUSDLFJSJ9877DLJFSLD9999SLDJFS";
var Appid = "51477108";
var access_token = "";
using (var wc = new WebClient())
{
var result = wc.DownloadString(string.Format("http://www.xxxxx.com?appid={0}&secret={1}", Appid, AppSecret));
if (result.Contains("40001"))
{
return;
}
access_token = JsonConvert.DeserializeObject<Token>(result).access_token;
}
if (string.IsNullOrEmpty(access_token))
{
return;
}
var Url = string.Format("http://www.xxxxx.com?access_token={0}", access_token);
//模拟数据
var dict = new Dictionary<string, string>();
//这是一个实体对象,传入的参数
dict.Add("姓名", companyUserName);
dict.Add("职务", post);
dict.Add("手机", mobile);
dict.Add("邮箱",email);
dict.Add("公司", companyType);
dict.Add("需求来源", saleClueSource);
var data = JsonConvert.SerializeObject(dict);
//数据签名
var sign = Getmd5(data + AppSecret).ToUpper();
try
{
var PostResutl = Post(Url, data, "application/json", 1000 * 3, Encoding.UTF8, "sign", sign);
}
catch (Exception ex)
{
}
}
public class Token
{
public string access_token { get; set; }
public int expires_in { get; set; }
}
public static string GetMd5(string inputStr)
{
byte[] md5Bytes = Encoding.UTF8.GetBytes(inputStr);
MD5 md5 = new MD5CryptoServicePRovider();
byte[] cryptString = md5.ComputeHash(md5Bytes);
int len;
StringBuilder sb = new StringBuilder();
len = cryptString.Length;
for (int i = 0; i < len; i++)
{
sb.Append(cryptString[i].ToString("X2"));
}
return sb.ToString();
}
public static string Post(string url, string data, string contentType, int timeoutMillis, Encoding encoding = null, string RequestKey = null, string RequestValue = null)
{
encoding = encoding ?? Encoding.UTF8;
var request = WebRequest.Create(url);
request.Method = "POST";
//request.Timeout = timeoutMillis;
request.ContentType = contentType;
if (!string.IsNullOrEmpty(RequestKey))
{
request.Headers.Add(RequestKey, RequestValue);
}
using (var reqStream = request.GetRequestStream())
{
using (var writer = new StreamWriter(reqStream, encoding))
{
writer.Write(data);
writer.Flush();
writer.Close();
}
}
using (var response = request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
if (stream == null)
{
return string.Empty;
}
using (var reader = new StreamReader(stream, encoding))
{
var s = reader.ReadToEnd();
return s;
}
}
}
}
}
}
View Code
接收接口的数据
public class HomeController : Controller
{
// GET: Home
[HttpPost]
public ActionResult Index()
{
//分配的AppSerect
var AppSerect = "dafsdfaijlasjdfsajfd";
if (string.IsNullOrEmpty(Request.Headers["sign"]))
{
//不是推送的数据
return null;
}
var sign = Request.Headers["sign"];
System.IO.Stream s = Request.InputStream;
var content = "";
using (var reader = new StreamReader(s, Encoding.UTF8))
{
content = reader.ReadToEnd();
}
var aydata = Newtonsoft.Json.JsonConvert.DeserializeObject<AYC>(content);
if (GetMd5(content + AppSerect) == sign)
{
//推送的数据
}
else
{
//不是推送的数据
}
return Content(content);
}
using System.Net;
using System.Security.Cryptography;
using System.IO;
using Newtonsoft.Json;
namespace CRM.BLL.SalesClue
{
public class AddSalesClueLogic
{
public void PushSaleClueData(string companyUserName,string mobile,string email,string post,string saleClueSource,string companyType)
{
//AppSecret
var AppSecret = "TUSDLFJSJ9877DLJFSLD9999SLDJFS";
var Appid = "51477108";
var access_token = "";
using (var wc = new WebClient())
{
var result = wc.DownloadString(string.Format("http://www.xxxxx.com?appid={0}&secret={1}", Appid, AppSecret));
if (result.Contains("40001"))
{
return;
}
access_token = JsonConvert.DeserializeObject<Token>(result).access_token;
}
if (string.IsNullOrEmpty(access_token))
{
return;
}
var Url = string.Format("http://www.xxxxx.com?access_token={0}", access_token);
//模拟数据
var dict = new Dictionary<string, string>();
//这是一个实体对象,传入的参数
dict.Add("姓名", companyUserName);
dict.Add("职务", post);
dict.Add("手机", mobile);
dict.Add("邮箱",email);
dict.Add("公司", companyType);
dict.Add("需求来源", saleClueSource);
var data = JsonConvert.SerializeObject(dict);
//数据签名
var sign = Getmd5(data + AppSecret).ToUpper();
try
{
var PostResutl = Post(Url, data, "application/json", 1000 * 3, Encoding.UTF8, "sign", sign);
}
catch (Exception ex)
{
}
}
public class Token
{
public string access_token { get; set; }
public int expires_in { get; set; }
}
public static string GetMd5(string inputStr)
{
byte[] md5Bytes = Encoding.UTF8.GetBytes(inputStr);
MD5 md5 = new MD5CryptoServicePRovider();
byte[] cryptString = md5.ComputeHash(md5Bytes);
int len;
StringBuilder sb = new StringBuilder();
len = cryptString.Length;
for (int i = 0; i < len; i++)
{
sb.Append(cryptString[i].ToString("X2"));
}
return sb.ToString();
}
public static string Post(string url, string data, string contentType, int timeoutMillis, Encoding encoding = null, string RequestKey = null, string RequestValue = null)
{
encoding = encoding ?? Encoding.UTF8;
var request = WebRequest.Create(url);
request.Method = "POST";
//request.Timeout = timeoutMillis;
request.ContentType = contentType;
if (!string.IsNullOrEmpty(RequestKey))
{
request.Headers.Add(RequestKey, RequestValue);
}
using (var reqStream = request.GetRequestStream())
{
using (var writer = new StreamWriter(reqStream, encoding))
{
writer.Write(data);
writer.Flush();
writer.Close();
}
}
using (var response = request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
if (stream == null)
{
return string.Empty;
}
using (var reader = new StreamReader(stream, encoding))
{
var s = reader.ReadToEnd();
return s;
}
}
}
}
}
}
View Code
接收接口的数据
public class HomeController : Controller
{
// GET: Home
[HttpPost]
public ActionResult Index()
{
//分配的AppSerect
var AppSerect = "dafsdfaijlasjdfsajfd";
if (string.IsNullOrEmpty(Request.Headers["sign"]))
{
//不是推送的数据
return null;
}
var sign = Request.Headers["sign"];
System.IO.Stream s = Request.InputStream;
var content = "";
using (var reader = new StreamReader(s, Encoding.UTF8))
{
content = reader.ReadToEnd();
}
var aydata = Newtonsoft.Json.JsonConvert.DeserializeObject<AYC>(content);
if (GetMd5(content + AppSerect) == sign)
{
//推送的数据
}
else
{
//不是推送的数据
}
return Content(content);
}
追问
能给点注释吗?
广东轻亿云软件
2024-05-14 广告
2024-05-14 广告
广东轻亿云软件科技有限公司在软件开发领域深耕多年,积累了丰富的经验和技术实力。我们深知API接口在现代软件开发中的重要性,因此,我们与多家业界领先的API接口提供商保持着紧密的合作关系,确保我们的产品和服务能够充分利用这些接口,为用户提供更...
点击进入详情页
本回答由广东轻亿云软件提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询