//沙箱环境 protected string AppKey = "102223556992"; protected string AppSecret = "sand3xc00f28d1f022ae6343ed69f5d"; protected void Page_Load(object sender, EventArgs e) { string url = "https-://oauth.tbsandbox.-com/authorize"; //沙箱环境 //参数 taobao.Model.parameterCode model = new Model.parameterCode() { client_id = AppKey, redirect_uri = "localhost:50138/OAuth.aspx", response_type = "code", state = "13", view = "web" }; url += ("?client_id=" + model.client_id + "&redirect_uri=" + model.redirect_uri + "&response_type=" + model.response_type + "&state=" + model.state + "&view=" + model.view); Response.Redirect(url); }
//沙箱环境 protected string AppKey = "102223556992"; protected string AppSecret = "sand3xc00f28d1f022ae6343ed69f5d"; protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString["code"] != null) { //获取到授权码Code,执行获取Access ToKen方法 GetAccessToKen(Request.QueryString["code"].ToString()); } } //获取Access ToKen令牌方法 protected void GetAccessToKen(string code) { string url = "https:-//oauth.tbsandbox.-com/token"; //沙箱环境 #region 参数整合 taobao.Model.parameterAccessToKen model = new Model.parameterAccessToKen() { client_id = AppKey, client_secret = AppSecret, code = code, grant_type = "authorization_code", redirect_uri = "localhost:50138/OAuth.aspx", state = "13", view = "web" }; string strCan = ""; strCan += ("client_id=" + model.client_id); strCan += ("&client_secret=" + model.client_secret); strCan += ("&code=" + model.code); strCan += ("&grant_type=" + model.grant_type); strCan += ("&redirect_uri=" + model.redirect_uri); strCan += ("&state=" + model.state); strCan += ("&view=" + model.view); #endregion Response.Write(GetPage(url, strCan)); } /// <summary> /// Post数据到网站 /// </summary> /// <param name="posturl">网址</param> /// <param name="postData">参数</param> /// <returns></returns> public string GetPage(string posturl, string postData) { Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; Encoding encoding = System.Text.Encoding.GetEncoding("UTF-8"); byte[] data = encoding.GetBytes(postData); // 准备请求... try { // 设置参数 request = WebRequest.Create(posturl) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); //发送请求并获取相应回应数据 response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回结果网页(html)代码 string content = sr.ReadToEnd(); string err = string.Empty; return content; } catch (Exception ex) { string err = ex.Message; return err; } }
但是,这个第二部怎么都弄好不!尤其是“通过Https Post方式换取Token”我都改了好多种POST的方法了,还是不行,每次都会报错“基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。”
人家用的ssl/tls
//如果是发送HTTPS请求
if(url.StartsWith("https",StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion=HttpVersion.Version10;
}
你post加上这个