需要命名空間 using System.Net; using System.Net.Security using System.Security.Cryptography.X509Certificates; using System.Web.Script.Serialization; using N ...
需要命名空間
using System.Net;
using System.Net.Security
using System.Security.Cryptography.X509Certificates;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
//來對遠程X.509證書的驗證進行處理防止出現未能為SSL/TLS 安全通道建立信任關係錯誤
public static class Util
{
public static void SetCertificatePolicy()
{
ServicePointManager.ServerCertificateValidationCallback
+= RemoteCertificateValidate;
}
private static bool RemoteCertificateValidate(
object sender, X509Certificate cert,
X509Chain chain, SslPolicyErrors error)
{
System.Console.WriteLine("Warning, trust any certificate");
return true;
}
}
public string Token()
{
string parameters = "acount=123&&pwd=456";//介面賬號密碼
string url = String.Concat("https://api.ceshi/login.json?", parameters);//獲取Token的API地址
Util.SetCertificatePolicy();//來對遠程X.509證書的驗證進行處理防止出現未能為SSL/TLS 安全通道建立信任關係
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//創建request
request.Method = "GET";//提交數據方式
HttpWebResponse response = (HttpWebResponse)request.GetResponse();//發送目標請求
string jsonString;//json字元串
string token;//token
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
jsonString = reader.ReadToEnd();//得到json字元串
JavaScriptSerializer js = new JavaScriptSerializer();
Test.info info = js.Deserialize<Test.info>(jsonString);// json格式實體 Deserialize<T>(String):將JSON字元串轉化為類型T。 將json字元串進行序列化為Test.info
token = info.data.community_token;//得到token
}
return token;
}