public static string InvokePostByHttpUtil(string url, MyFormUrlEncodedContent parameters) { MyHttpClient myHttpClient = MyHttpClient.GetInstance(); Task <HttpResponseMessage> task = myHttpClient.PostAsync(url, parameters); HttpResponseMessage response = task.Result; var result = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); return(result); }
private static void LoginByHttpUtil(string url, string username, string password, string sessionID) { var parameters = new Dictionary <string, string> { { "ses_id", sessionID }, { "loginid", username }, { "password", password }, { "loginforid", "1" }, { "loginbtn", "%83%8D%83O%83C%83%93" } }; var encodedContent = new MyFormUrlEncodedContent(parameters); var result = InvokePostByHttpUtil(url, encodedContent); if (result.Contains("#changeLogin")) { throw new Exception("用户名与密码出现问题!"); } }
public static List <string> UploadPictureToWebsite(List <Image> pictureImages) { List <string> list = new List <string>(); foreach (Image pictureImage in pictureImages) { var parameters = new Dictionary <string, string>(); string base64PictrueStr = GetBase64FromImage(pictureImage); parameters.Add("dataUrl", base64PictrueStr); parameters.Add("block", "0"); var encodedContent = new MyFormUrlEncodedContent(parameters); string result = InvokePostByHttpUtil(Url.UPLOAD_IMAGE, encodedContent); string imageUrl = GetImageUrlFromResponse(result); list.Add(imageUrl); } return(list); }
private static byte[] GetContentByteArray(IEnumerable <KeyValuePair <string, string> > nameValueCollection) { if (nameValueCollection == null) { throw new ArgumentNullException("nameValueCollection"); } StringBuilder stringBuilder = new StringBuilder(); foreach (KeyValuePair <string, string> current in nameValueCollection) { if (stringBuilder.Length > 0) { stringBuilder.Append('&'); } stringBuilder.Append(MyFormUrlEncodedContent.Encode(current.Key)); stringBuilder.Append('='); stringBuilder.Append(MyFormUrlEncodedContent.Encode(current.Value)); } return(Encoding.Default.GetBytes(stringBuilder.ToString())); }
public MyFormUrlEncodedContent(IEnumerable <KeyValuePair <string, string> > nameValueCollection) : base(MyFormUrlEncodedContent.GetContentByteArray(nameValueCollection)) { base.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); }