string token = getMd5("app_smtj" + "&" + "timeStamp=" + currenttimemillis + "&" + "6919c03c6f9349ccacc1a2a1096fd045");
方法:
protected string getMd5(string s)
{
//初始化MD5對象
MD5 md5 = MD5.Create();
//将源字元串轉化為byte數組
Byte[] soucebyte = Encoding.Default.GetBytes(s);
//soucebyte轉化為mf5的byte數組
Byte[] md5bytes = md5.ComputeHash(soucebyte);
//将md5的byte數組再轉化為MD5數組
StringBuilder sb = new StringBuilder();
foreach (Byte b in md5bytes)
{
//x表示16進制,2表示2位
sb.Append(b.ToString("x2"));
}
return sb.ToString();
}
PostWebRequestToken方法:
public static string PostWebRequestToken(string postUrl, string token, string paramData, bool isReplaceEmpty = true)
{
string ret = string.Empty;
try
{
if (isReplaceEmpty)
{
paramData = paramData.Replace(" ", "");
}
Encoding dataEncode = Encoding.UTF8;
byte[] byteArray = dataEncode.GetBytes(paramData); //轉化
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
webReq.Method = "POST";
webReq.Headers.Add("accept-charset", "UTF-8");
//webReq.Headers.Add("X-Subject-Token", token);
#region 填充httpWebRequest的基本資訊
webReq.ContentType = "application/json";
#endregion
webReq.ContentLength = byteArray.Length;
Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);//寫入參數
newStream.Close();
using (HttpWebResponse response = (HttpWebResponse)webReq.GetResponse())
{
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
ret = sr.ReadToEnd();
sr.Close();
response.Close();
}
newStream.Close();
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
return ret;
}
那麼怎麼用呢? 我寫的一個完整方法 ,有幾個參數需要傳入,其他的是多餘的,
在調試的過程中,可以把這個參數拿出來到postman執行以下,看看是否正确。
string content = "{\n" +
"\"appId\" : \" " + "app_smtj" + "\"" + douhao + "\n" +
"\"appSecret\" :\"" + "6919c03c6f9349ccacc1a2a1096fd045" + "\" " + douhao + " \n" +
"\"timeStamp\" :\"" + "1594865506" + "\"" + douhao + "\n" +
//"\"areaCode\" :[350203]\n" +
"\"orgCode\" :\"" + "350203291100" + "\"" + douhao + "\n" +
"\"token\" :\"" + "77ab90c83db02e3a18fe7907d63f0b89" + "\"\n" +
"}";
根據接口要求來寫,
private void GetpersonNum()
{
object currenttimemillis = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds / 1000;
string token = getMd5("app_smtj" + "&" + "timeStamp=" + currenttimemillis + "&" + "6919c03c6f9349ccacc1a2a1096fd045");
try
{
personNumResultModel rqm = new personNumResultModel();
string postData = JsonConvert.SerializeObject(rqm);
string url = personNum;
string data = ApiHelper.Post(url, postData);
//personNumResultModel rm = JsonConvert.DeserializeObject<personNumResultModel>(data);
List<personNumSqlModel> list = new List<personNumSqlModel>();
string douhao = ",";
string content = "{\n" +
"\"appId\" : \" " + "app_smtj" + "\"" + douhao + "\n" +
"\"appSecret\" :\"" + "6919c03c6f9349ccacc1a2a1096fd045" + "\" " + douhao + " \n" +
"\"timeStamp\" :\"" + "1594865506" + "\"" + douhao + "\n" +
//"\"areaCode\" :[350203]\n" +
"\"orgCode\" :\"" + "350203291100" + "\"" + douhao + "\n" +
"\"token\" :\"" + "77ab90c83db02e3a18fe7907d63f0b89" + "\"\n" +
"}";
var response = ConnectHelper.PostWebRequestToken(personNum, token, content);
personNumResultModel rm = JsonConvert.DeserializeObject<personNumResultModel>(response);
#region MyRegion
if (rm != null && rm.data != null)
{
personNumSqlModel sm = new personNumSqlModel();
sm.zzllZcs = rm.data.zzllZcs;
sm.jhzs = rm.data.jhzs;
sm.shllJhs = rm.data.shllJhs;
sm.fzllZcs = rm.data.fzllZcs;
sm.zczs = rm.data.zczs;
sm.shllZcs = rm.data.shllZcs;
sm.zzllJhs = rm.data.zzllJhs;
sm.fzllJhs = rm.data.fzllJhs;
sm.response_time = rm.response_time;
sm.jsessionid = rm.jsessionid;
sm.SJ = DateTime.Now;
list.Add(sm);
string namse = DataHelper.GetSqlNamesByModel(new personNumSqlModel());
string values = DataHelper.GetSqlValuesByModel(list, new personNumSqlModel());
MySqlHelper.ClearDataByName("zdcw_tb_jk1", connection);
MySqlHelper.InsertDataByName("zdcw_tb_jk1", namse, values, connection);
Console.WriteLine(DateTime.Now + "群治人員數量區分注冊數、 激活數,區分類型,按區、街道(派出所)、社群統計資料入庫成功");
}
else
{
Console.WriteLine(DateTime.Now + "群治人員數量區分注冊數、 激活數,區分類型,按區、街道(派出所)、社群統計無資料");
}
#endregion
}
catch (Exception ex)
{
Console.WriteLine("Error___群治人員數量區分注冊數、 激活數,區分類型,按區、街道(派出所)、社群統計" + DateTime.Now + ex.Message);
WriteLogsHelper.ErrorLogs("群治人員數量區分注冊數、 激活數,區分類型,按區、街道(派出所)、社群統計", ex.Message, "群治人員數量區分注冊數、 激活數,區分類型,按區、街道(派出所)、社群統計");
}
}