EMQ進行HttpApi登陸問題

今天進行EMQ http api調用的時候遇到一個問題,一直彈出登陸驗證框html

在官網資料中也找不到相關的接口,以下圖:json

之前也常常看到這種登陸,不過我這裏沒有用程序去調用過這樣相似的接口.c#

後來我想到常常在用迅雷下載一些電影的時候,在迅雷地址中看到過一種ftp的寫法,如: ftp:帳號:密碼@xxx.com/xyz.mp4如此如此.api

我就嘗試了一下帳號密碼登陸,竟然成功了.網絡

這樣一來我百度的範圍就縮小了,找到了一個關鍵詞 -----> HTTP Authorizationapp

原來是這玩意搞的鬼ide

記錄一下分享給你們.加密

如下內容來自網絡:https://www.cnblogs.com/forydb/p/10000301.htmlspa

c#項目中用到調用客戶接口,basic身份認證,base64格式加密(用戶名:密碼)貼上代碼以備後用code

一、使用HttpClient實現basic身份認證

1 using (HttpClient client = new HttpClient())
2 {
3     client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Username}:{Password}")));
4     HttpContent httpContent = new StringContent("", Encoding.UTF8);
5     httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
6     Uri address = new Uri("接口地址");
7     var response = client.PostAsync(address, httpContent).Result.Content.ReadAsStringAsync().Result;//返回值
8 }
使用HttpClient實現basic身份認證

 

二、使用HttpWebRequest實現basic身份認證

 1 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("接口地址");
 2 request.Method = "Post";
 3 request.CookieContainer = new CookieContainer();
 4 request.ContentType = "application/json;";
 5 
 6 //(1)設置請求Credentials
 7 CredentialCache credentialCache = new CredentialCache();
 8 credentialCache.Add(new Uri("接口地址"), "Basic", new NetworkCredential("用戶名", "密碼"));
 9 request.Credentials = credentialCache;
10 
11 //(2)設置Headers Authorization
12 request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Username}:{Password}")));
13 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
14 {
15     using (StreamReader reader = new StreamReader(response.GetResponseStream()))
16     {
17         string content = reader.ReadToEnd();
18     }
19 }
使用HttpWebRequest實現basic身份認證
相關文章
相關標籤/搜索