在一次项目中,在给客户做系统时,需要调用客户的系统获取数据,客户提供了获取Token和获取数据的接口,用户名和密码,认证方式是基于Bearer的,通过学习和找资料,提供一个C#示例
一个C#示例
1、传入语句获取数据
public async Task<string > ThirdSystemDataPostAsync (string param,string dataUrlAddress )
{
string token = await Bearer_TokenPostAsync();
_logger.Warn($"获取Token结构:{token} " );
string responseString = string .Empty;
try
{
var tokenJson = JsonConvert.DeserializeObject<TokenResultDto>(token);
_logger.Warn($"获取Token值参数据:{tokenJson} " );
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true ;
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(dataUrlAddress);
_logger.Warn($"入参数据:{param} " );
_logger.Warn($"智慧系统数据地址:{dataUrlAddress} " );
httpRequest.Method = "Post" ;
httpRequest.ContentType = " application/json; charset=utf-8" ;
httpRequest.Headers.Add("Authorization" , "Bearer " + tokenJson.accessToken);
byte [] bytesRequestData = Encoding.UTF8.GetBytes(param);
httpRequest.ContentLength = bytesRequestData.Length;
Stream postStream = await httpRequest.GetRequestStreamAsync();
postStream.Write(bytesRequestData, 0 , bytesRequestData.Length);
postStream.Close();
SetWebRequest(httpRequest);
using (HttpWebResponse myResponse = (HttpWebResponse)httpRequest.GetResponse())
{
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
responseString = sr.ReadToEnd();
_logger.Warn($"调用智慧系统返回结果数据:{responseString} " );
return responseString;
}
}
catch (Exception ex)
{
responseString = ex.ToString();
}
return responseString;
}
2、获取token
private async Task<string > Bearer_TokenPostAsync (string sSoUrlAddress,string userName,string userPassword )
{
string responseString = string .Empty;
try
{
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(sSoUrlAddress);
_logger.Warn($"智慧系统Token地址:{sSoUrlAddress} " );
httpRequest.Method = "Post" ;
httpRequest.ContentType = " application/json; charset=utf-8" ;
var usermesg = new UserMesgInput()
{
Username = userName,
Password = userPassword,
UserDetail = true
};
string base64 = JsonConvert.SerializeObject(usermesg);
byte [] bytesRequestData = Encoding.UTF8.GetBytes(base64);
httpRequest.ContentLength = bytesRequestData.Length;
Stream postStream = await httpRequest.GetRequestStreamAsync();
postStream.Write(bytesRequestData, 0 , bytesRequestData.Length);
postStream.Close();
SetWebRequest(httpRequest);
_logger.Warn($"获取设置身份认证及请求超时时间" );
using (HttpWebResponse myResponse = (HttpWebResponse)httpRequest.GetResponse())
{
_logger.Warn($"StreamReader对象" );
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
responseString = sr.ReadToEnd();
_logger.Warn($"调用智慧系统返回结果Token:{responseString} " );
}
}
catch (Exception ex)
{
responseString = ex.ToString();
}
return responseString;
}
3、获取设置身份认证及请求超时时间
private static void SetWebRequest (HttpWebRequest request )
{
request.Credentials = CredentialCache.DefaultCredentials;
request.Timeout = 1000000 ;
}
4、在我使用的时候出现了下面的错误,通过查找是证书的问题,因为客户的web访问使用的IP地址访问,所以在代码中要加入跳过ssl验证的一行代码
System.Net.WebException: The SSL connection could not be established, see inner exception. The remote certificate is invalid according to the validation procedure .
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?