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
- {
-
-
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
-
-
- public static string Post(string uri, string paramStr, string username, string password)
- {
- string result = string.Empty;
- WebClient client = new WebClient();
-
- 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);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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));
- }
- }
- }
|