以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- c# 转foxtable (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=175018) |
-- 作者:lshshlxsh -- 发布时间:2022/2/16 13:32:00 -- c# 转foxtable 请问老师下面c#转出foxtable 要怎么写? using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Security.Cryptography; using System.Text; namespace MyDemo { class Program { public static string HttpGet(string url, string contentType) { if (url.StartsWith("https")) { System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; } HttpClient httpClient = new HttpClient(); if (!string.IsNullOrWhiteSpace(contentType)) { httpClient.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue(contentType)); } HttpResponseMessage response = httpClient.GetAsync(url).Result; string result = response.Content.ReadAsStringAsync().Result; if (response.IsSuccessStatusCode) { return result; } else { throw new Exception(result); } } public static string HttpPost(String url, string body, string contentType) { HttpWebResponse httpWebResponse = null; try { HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.Method = "POST"; httpWebRequest.ContentType = contentType; httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36"; httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; httpWebRequest.CookieContainer = new CookieContainer(); byte[] btBodys = Encoding.UTF8.GetBytes(body); httpWebRequest.ContentLength = btBodys.Length; httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length); httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()); string responseContent = streamReader.ReadToEnd(); httpWebResponse.Close(); streamReader.Close(); httpWebResponse.Close(); httpWebRequest.Abort(); return responseContent; } catch (Exception ex) { throw ex; } finally { if (httpWebResponse != null) { httpWebResponse.Close(); } } } public static string Md5(string strInput) { byte[] result = Encoding.Default.GetBytes(strInput.Trim()); //tbPass为输入密码的文本框 MD5 md5 = new MD5CryptoServiceProvider(); byte[] output = md5.ComputeHash(result); return BitConverter.ToString(output).Replace("-", ""); } /// <summary> /// 构造访问的url /// </summary> /// <param name="paramsMap">参数列表</param> /// <param name="isSign">是否构造签名串</param> /// <returns></returns> private static string GetRequestQueryString(Dictionary<string, string> paramsMap, bool isSign) { StringBuilder sb = new StringBuilder(); List<string> list = paramsMap.Keys.ToList(); if (isSign) { list.Sort(); //排序参数 } foreach (string key1 in list) { sb.Append("&"); sb.Append(key1); sb.Append("="); if (isSign) { sb.Append(paramsMap[key1]); } else { sb.Append(Uri.EscapeDataString(paramsMap[key1])); } } return sb.ToString().Substring(1); } private static string InvokeOpenApi(HttpMethod httpMethod, string openUrl, string userId, string key, string method, string format, string version, Dictionary<string, string> param) { Dictionary<string, string> paramsMap = new Dictionary<string, string>(); String timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); paramsMap.Add("user_id", userId); paramsMap.Add("format", format); paramsMap.Add("timestamp", timestamp); paramsMap.Add("method", method); if (version.Equals("2.0")) { paramsMap.Add("version", version); //2.0版本增加version参数 } foreach (string paramkey in param.Keys) { paramsMap.Add(paramkey, param[paramkey]); //将外部参数也加入到列表中 } if (version.Equals("2.0")) { string urlString = GetRequestQueryString(paramsMap, true); urlString = urlString + "&key=" + key; string sign = Md5(urlString).ToUpper(); paramsMap.Add("sign", sign); } else if (version.Equals("1.0")) { string sign = Md5(userId + key + timestamp).ToUpper(); paramsMap.Add("sign", sign); } string result = ""; string invokeUrl = GetRequestQueryString(paramsMap, false); if (httpMethod == HttpMethod.Get) { result = HttpGet(openUrl + "?" + invokeUrl, "application/json"); } else if (httpMethod == HttpMethod.Post) { result = HttpPost(openUrl, invokeUrl, "application/x-www-form-urlencoded"); } else { throw new Exception("invokeOpenApi 2.0 not support httpmethod" + httpMethod.ToString()); } return result; } static void Main(string[] args) { string releaseApiUrl = "http://api.nbeport.com/router/rest"; string debugApiUrl = "http://api.trainer.nbeport.com/router/rest"; string userId = "xxxxx"; string key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; Dictionary<String, String> fun1Map = new Dictionary<string, string>(); fun1Map.Add("userid", "test"); string response1 = InvokeOpenApi(HttpMethod.Get, releaseApiUrl, userId, key, "nbeport.cas.user.get", "json", "1.0", fun1Map); Console.Write(response1); Console.WriteLine(); Dictionary<String, String> fun2Map = new Dictionary<string, string>(); fun2Map.Add("number", "ONEYNB9BHC295400"); string response2 = InvokeOpenApi(HttpMethod.Get, releaseApiUrl, userId, key, "eportyun.logistics.subscribe.get", "json", "2.0", fun2Map); Console.Write(response2); Console.ReadLine(); } } } |
-- 作者:有点蓝 -- 发布时间:2022/2/16 13:38:00 -- c#转换vb参考:https://converter.telerik.com/ 不过这些代码使用了高版本.net的语法,foxtable是用不了的
|
-- 作者:lshshlxsh -- 发布时间:2022/2/16 13:56:00 -- 谢谢老师 这个转出来是 vb.net , 这个要怎么转换成狐表? Imports System Imports System.Collections.Generic Imports System.IO Imports System.Linq Imports System.Net Imports System.Net.Http Imports System.Net.Http.Headers Imports System.Security.Cryptography Imports System.Text Namespace MyDemo Class Program Public Shared Function HttpGet(ByVal url As String, ByVal contentType As String) As String If url.StartsWith("https") Then System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls End If Dim httpClient As HttpClient = New HttpClient() If Not String.IsNullOrWhiteSpace(contentType) Then httpClient.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue(contentType)) End If Dim response As HttpResponseMessage = httpClient.GetAsync(url).Result Dim result As String = response.Content.ReadAsStringAsync().Result If response.IsSuccessStatusCode Then Return result Else Throw New Exception(result) End If End Function Public Shared Function HttpPost(ByVal url As String, ByVal body As String, ByVal contentType As String) As String Dim httpWebResponse As HttpWebResponse = Nothing Try Dim httpWebRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest) httpWebRequest.Method = "POST" httpWebRequest.ContentType = contentType httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36" httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" httpWebRequest.CookieContainer = New CookieContainer() Dim btBodys As Byte() = Encoding.UTF8.GetBytes(body) httpWebRequest.ContentLength = btBodys.Length httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length) httpWebResponse = CType(httpWebRequest.GetResponse(), HttpWebResponse) Dim streamReader As StreamReader = New StreamReader(httpWebResponse.GetResponseStream()) Dim responseContent As String = streamReader.ReadToEnd() httpWebResponse.Close() streamReader.Close() httpWebResponse.Close() httpWebRequest.Abort() Return responseContent Catch ex As Exception Throw ex Finally If httpWebResponse IsNot Nothing Then httpWebResponse.Close() End If End Try End Function Public Shared Function Md5(ByVal strInput As String) As String Dim result As Byte() = Encoding.[Default].GetBytes(strInput.Trim()) Dim md5 As MD5 = New MD5CryptoServiceProvider() Dim output As Byte() = md5.ComputeHash(result) Return BitConverter.ToString(output).Replace("-", "") End Function Private Shared Function GetRequestQueryString(ByVal paramsMap As Dictionary(Of String, String), ByVal isSign As Boolean) As String Dim sb As StringBuilder = New StringBuilder() Dim list As List(Of String) = paramsMap.Keys.ToList() If isSign Then list.Sort() End If For Each key1 As String In list sb.Append("&") sb.Append(key1) sb.Append("=") If isSign Then sb.Append(paramsMap(key1)) Else sb.Append(Uri.EscapeDataString(paramsMap(key1))) End If Next Return sb.ToString().Substring(1) End Function Private Shared Function InvokeOpenApi(ByVal httpMethod As HttpMethod, ByVal openUrl As String, ByVal userId As String, ByVal key As String, ByVal method As String, ByVal format As String, ByVal version As String, ByVal param As Dictionary(Of String, String)) As String Dim paramsMap As Dictionary(Of String, String) = New Dictionary(Of String, String)() Dim timestamp As String = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") paramsMap.Add("user_id", userId) paramsMap.Add("format", format) paramsMap.Add("timestamp", timestamp) paramsMap.Add("method", method) If version.Equals("2.0") Then paramsMap.Add("version", version) End If For Each paramkey As String In param.Keys paramsMap.Add(paramkey, param(paramkey)) Next If version.Equals("2.0") Then Dim urlString As String = GetRequestQueryString(paramsMap, True) urlString = urlString & "&key=" & key Dim sign As String = Md5(urlString).ToUpper() paramsMap.Add("sign", sign) ElseIf version.Equals("1.0") Then Dim sign As String = Md5(userId & key & timestamp).ToUpper() paramsMap.Add("sign", sign) End If Dim result As String = "" Dim invokeUrl As String = GetRequestQueryString(paramsMap, False) If httpMethod = HttpMethod.[Get] Then result = HttpGet(openUrl & "?" & invokeUrl, "application/json") ElseIf httpMethod = HttpMethod.Post Then result = HttpPost(openUrl, invokeUrl, "application/x-www-form-urlencoded") Else Throw New Exception("invokeOpenApi 2.0 not support httpmethod" & httpMethod.ToString()) End If Return result End Function Private Shared Sub Main(ByVal args As String()) Dim releaseApiUrl As String = "http://api.nbeport.com/router/rest" Dim debugApiUrl As String = "http://api.trainer.nbeport.com/router/rest" Dim userId As String = "xxxxx" Dim key As String = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" Dim fun1Map As Dictionary(Of String, String) = New Dictionary(Of String, String)() fun1Map.Add("userid", "test") Dim response1 As String = InvokeOpenApi(HttpMethod.[Get], releaseApiUrl, userId, key, "nbeport.cas.user.get", "json", "1.0", fun1Map) Console.Write(response1) Console.WriteLine() Dim fun2Map As Dictionary(Of String, String) = New Dictionary(Of String, String)() fun2Map.Add("number", "ONEYNB9BHC295400") Dim response2 As String = InvokeOpenApi(HttpMethod.[Get], releaseApiUrl, userId, key, "eportyun.logistics.subscribe.get", "json", "2.0", fun2Map) Console.Write(response2) Console.ReadLine() End Sub End Class End Namespace [此贴子已经被作者于2022/2/16 14:12:51编辑过]
|
-- 作者:有点蓝 -- 发布时间:2022/2/16 14:13:00 -- foxtable使用的就是vb.net。正常把Class Program以及里面的内容放到全局代码,然后补齐命名空间即可。 不过这些代码使用了高版本.net的语法,foxtable是用不了的
|
-- 作者:lshshlxsh -- 发布时间:2022/2/16 14:27:00 -- 谢谢老师 |