using BizCom; using Microsoft.SqlServer.Server; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using NHibernate.Cache; using SiteCore.Handler; using SiteCore.taoObj; using SiteCore.taoObj.WebApi; using SQLData; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Data; using System.Diagnostics; using System.IO; using System.Linq; using System.Net; using System.Net.Http.Headers; using System.Net.Http; using System.Reflection; using System.Security.Cryptography.Xml; using System.Security.Policy; using System.Text; using System.Threading.Tasks; using System.Web; using static SiteCore.taoObj.Api_trade_info; using System.Text.RegularExpressions; using NPOI.OpenXmlFormats.Shared; namespace SiteCore.Handler { public class apichyHelper : BaseHandler, IHttpHandler { public void ProcessRequest(HttpContext context) { if (UrlParmsCheck("t")) { String tname = GetString("t"); MethodInfo method; Type type = this.GetType(); method = type.GetMethod(tname); con = context; if (method == null) { conError("找不到对应方法,服务器返回错误"); return; } successFlag = false; context.Response.ContentType = "application/json"; context.Response.Headers.Set("Access-Control-Allow-Origin", "*"); try { method.Invoke(this, null); } catch (Exception ex) { conError("处理接口错误,服务器返回错误"); } finally { //AfterInvoke(methodName); } //if (!successFlag) returnErrorMsg("服务器返回错误"); } } /* static string pUrl = "http://tst.ds.gw.chenghuiyin.com/ordering"; static string userName = "001008"; static string password = "123234";*/ static string pUrl = "http://ds.gw.chenghuiyin.com/ordering"; static string userName = "001008"; static string password = "lt666888"; public static string getToken() { //return "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiYXBpIl0sInNjb3BlIjpbInJlYWQiLCJ3cml0ZSJdLCJleHAiOjE3MzU5NjU0ODAsImF1dGhvcml0aWVzIjpbIm9wZW5hcGkiXSwianRpIjoiYjY4NTRhZjctZjNmMy00OTU0LTk4ZmUtZDRlZTE4ZDllNzQyIiwiY2xpZW50X2lkIjoieGNsaWVudCJ9.E6U0NVMZPcUypl4P7L5718J1X8UxSJg9R7vZX_oMkuEJ-yelNXuYwu7-eAi1JTgIJ0GuuviQu-KEAH-W7sHb7t-JJJc-wLPMsLKmpwQDG2EpiRK5hW5ZmyUgL8rPuTUawKVP5k8SGrN60sVyXA2zKm4ztEQ39EGudXnam1Lld2w"; string token = ""; if (RedisHelper.HasKey("chy_token")) { token = RedisHelper.StringGet("chy_token").ToString(); { return token; } } string post_url = pUrl + "/api/User/GetToken"; string remoteInfo = ""; try { remoteInfo = httpGetTokenContent(post_url, JsonConvert.SerializeObject(new { userName, password })); if (!string.IsNullOrEmpty(remoteInfo)) { ChyTokenResponse chyToken = JsonConvert.DeserializeObject(remoteInfo); token = chyToken.accessToken; TimeSpan timeSpan = TimeSpan.FromSeconds(chyToken.expiresIn - 5); RedisHelper.StringSet("chy_token", token, timeSpan); } } catch (Exception ex) { XLog.SaveLog(0, ex.Message); } return token; } public static string updateExpressMark(string customerOrderId, string deliveryWayName) { string post_url = pUrl + "/api/ExpressAddress/UpdateExpressMark"; string remoteInfo = ""; string result = "修改成功"; try { remoteInfo = httpContent(post_url, JsonConvert.SerializeObject(new { customerOrderId, deliveryWayName })); if (!string.IsNullOrEmpty(remoteInfo)) { result = "修改失败"; } } catch (Exception ex) { result = "修改失败"; } return result; } static HttpClient client = new HttpClient(); private static string httpContent(string post_url, string content) { string token = getToken(); var request = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri(post_url), Headers = { { "Authorization", "bearer " + token }, { "ContentType", "application/json" }, }, Content = new StringContent(content) { Headers = { ContentType = new MediaTypeHeaderValue("application/json") } } }; var task = Task.Run(() => { try { var response = client.SendAsync(request).Result; response.EnsureSuccessStatusCode(); return response.Content.ReadAsStringAsync().Result; } catch (Exception e) { XLog.SaveLog(0, "chyhttpContent:" + e.Message); return "找不到商品或已发货"; } }); return task.Result; } private static string httpGetTokenContent(string post_url, string content) { var request = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri(post_url), Headers = { { "ContentType", "application/json" }, }, Content = new StringContent(content) { Headers = { ContentType = new MediaTypeHeaderValue("application/json") } } }; var task = Task.Run(() => { try { var response = client.SendAsync(request).Result; response.EnsureSuccessStatusCode(); return response.Content.ReadAsStringAsync().Result; } catch (Exception e) { XLog.SaveLog(0, "chyhttpContent:" + e.Message); return ""; } }); return task.Result; } } }