123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Text;
- using System.Threading.Tasks;
- namespace Ropin.Core.Common
- {
- public class HttpHelper
- {
- /// <summary>
- ///
- /// </summary>
- /// <param name="uri"></param>
- /// <param name="username"></param>
- /// <param name="password"></param>
- /// <returns></returns>
- public static string Get(string uri, string username, string password)
- {
- string result = string.Empty;
- WebClient client = new WebClient();
- if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
- {
- client.Credentials = GetCredentialCache(uri, username, password);
- client.Headers.Add("Authorization", GetAuthorization(username, password));
- }
- return client.DownloadString(uri);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="uri"></param>
- /// <param name="paramStr"></param>
- /// <param name="username"></param>
- /// <param name="password"></param>
- /// <returns></returns>
- public static string Post(string uri, string paramStr, string username, string password)
- {
- string result = string.Empty;
- WebClient client = new WebClient();
- // 采取POST方式必须加的Header
- client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
- byte[] postData = Encoding.UTF8.GetBytes(paramStr);
- if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
- {
- client.Credentials = GetCredentialCache(uri, username, password);
- client.Headers.Add("Authorization", GetAuthorization(username, password));
- }
- byte[] responseData = client.UploadData(uri, "POST", postData); // 得到返回字符流
- return Encoding.UTF8.GetString(responseData);// 解码
- }
- //public static string Post2(string uri, string paramStr, string username, string password)
- //{
- // HttpClient httpClient = httpClientFactory.CreateClient();
- // var msgData = new TpushMsgModel
- // {
- // C_DevStoreCode = devstore.C_ID,
- // C_MsgTypeCode = "MSG_TYPE_012",
- // Msg = devstore.C_Name + "的" + (string)ala["name"] + "设备点报警" + " 设备点值:" + (string)ala["value"],
- // Subject = "设备点报警",
- // DevNumber = devstore.C_NumberCode,
- // DevName = devstore.C_Name,
- // CreateOn = DateTime.Now.ToString(),
- // UserName = "设备",
- // };
- // var httpRequestMessage = new HttpRequestMessage
- // {
- // Method = HttpMethod.Post,
- // RequestUri = new Uri(uri),
- // Content = new StringContent(JsonConvert.SerializeObject(msgData), Encoding.UTF8, "application/json")
- // };
- // var response = await httpClient.SendAsync(httpRequestMessage);
- // string responseResult = await response.Content.ReadAsStringAsync();
- // if (response.StatusCode != HttpStatusCode.OK)
- // {
- // }
- // else
- // {
- // //todo
- // }
- //}
- private static CredentialCache GetCredentialCache(string uri, string username, string password)
- {
- string authorization = string.Format("{0}:{1}", username, password);
- CredentialCache credCache = new CredentialCache();
- credCache.Add(new Uri(uri), "Basic", new NetworkCredential(username, password));
- return credCache;
- }
- private static string GetAuthorization(string username, string password)
- {
- string authorization = string.Format("{}:{1}", username, password);
- return "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(authorization));
- }
- }
- }
|