コード例 #1
0
        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);
        }
コード例 #2
0
        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("用户名与密码出现问题!");
            }
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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()));
        }
コード例 #5
0
 public MyFormUrlEncodedContent(IEnumerable <KeyValuePair <string, string> > nameValueCollection)
     : base(MyFormUrlEncodedContent.GetContentByteArray(nameValueCollection))
 {
     base.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
 }