Example #1
1
 /// <summary>
 /// Gets the request.
 /// </summary>
 /// <param name="url">The URL.</param>
 /// <param name="requestParamDic">The request parameter dic.</param>
 /// <returns></returns>
 public Dictionary<string, string> postRequest(string url, Dictionary<string, string> requestParamDic)
 {
     httpPost = new HttpPost(new Uri(url));
     foreach (KeyValuePair<string, string> entry in requestParamDic)
     {
         httpPost.Parameters.Add(entry.Key, entry.Value);
     }
     //Get the response
     response = client.Execute(httpPost);
     //Get the response string
     string responseString = EntityUtils.ToString(response.Entity);
     //Get the Response Code and Save it
     string responseCode = response.ResponseCode.ToString();
     //Parse the response and return the values in the Dictionary
     Dictionary<String, String> responseCollection = null;
     if (responseCode == "200" && response != null)
     {
         //Console.WriteLine("Received the response" + responseString);
         responseCollection = new Dictionary<string, string>();
     }
     else
     {
         //Console.WriteLine("No Result returned");
     }
     return responseCollection;
 }
Example #2
0
        public static void SendWelcomeMail(string userName,string email)
        {
            HttpClient client = new HttpClient();
            HttpPost postMethod = new HttpPost(new Uri("http://sendcloud.sohu.com/webapi/mail.send_template.json"));
            MultipartEntity multipartEntity = new MultipartEntity();
            postMethod.Entity = multipartEntity;
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "template_invoke_name", "superjokes_register"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "substitution_vars", "{\"to\": [\"" + email + "\"], \"sub\" : { \"%username%\" : [\"" + userName + "\"]}}"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_user", "superjokes_cn"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_key", AppConfig.SendCloudKey));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "from", "*****@*****.**"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "fromname", "超级冷笑话"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "subject", "超级冷笑话注册邮件"));
            CodeScales.Http.Methods.HttpResponse response = client.Execute(postMethod);

            var repCode = response.ResponseCode;
            var repResult = EntityUtils.ToString(response.Entity);
            //LogHelper.Info("reg:" + email + repResult + AppConfig.SendCloudKey);

            //Console.WriteLine("Response Code: " + response.ResponseCode);
            //Console.WriteLine("Response Content: " + EntityUtils.ToString(response.Entity));

            //Response.Write("Response Code: " + response.ResponseCode);
            //Response.Write("<br/>");
            //Response.Write("Response Content: " + EntityUtils.ToString(response.Entity));
        }
        public void HttpPostWithFilesAndParameters()
        {
            // this method sends an empty file (or no file :)
            HttpClient client = new HttpClient();
            HttpPost postMethod = new HttpPost(new Uri(Constants.HTTP_MULTIPART_POST_200));

            MultipartEntity multipartEntity = new MultipartEntity();
            StringBody stringBody1 = new StringBody(Encoding.ASCII, "param1", "value1");
            multipartEntity.AddBody(stringBody1);
            StringBody stringBody2 = new StringBody(Encoding.ASCII, "param2", "!#$^&*((<>");
            multipartEntity.AddBody(stringBody2);
            FileBody fileBody1 = new FileBody("photo", "me.file", null);
            multipartEntity.AddBody(fileBody1);
            postMethod.Entity = multipartEntity;
            HttpResponse response = client.Execute(postMethod);

            Assert.AreEqual(200, response.ResponseCode);
            string responseString = EntityUtils.ToString(response.Entity);
            MessageData md = new MessageData();
            md.PostParameters.Add(new NameValuePair("param1", "value1"));
            md.PostParameters.Add(new NameValuePair("param2", "!#$^&*((<>"));
            md.Files.Add(new NameValuePair("me.file", "0"));
            Assert.AreEqual(md.ToString(), responseString);
            Assert.AreEqual(Constants.HTTP_MULTIPART_POST_200, response.RequestUri.AbsoluteUri);
            Console.Write(responseString);
        }
Example #4
0
        public static void DoPost()
        {
            HttpClient client = new HttpClient();
            HttpPost postMethod = new HttpPost(new Uri("http://www.w3schools.com/asp/demo_simpleform.asp"));

            List<NameValuePair> nameValuePairList = new List<NameValuePair>();
            nameValuePairList.Add(new NameValuePair("fname", "brian"));
            
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, Encoding.UTF8);
            postMethod.Entity = formEntity;
            
            HttpResponse response = client.Execute(postMethod);

            Console.WriteLine("Response Code: " + response.ResponseCode);
            Console.WriteLine("Response Content: " + EntityUtils.ToString(response.Entity));
        }
Example #5
0
        public static void VerifyNotice(string userName,string email,string jokeTitle,string jokeurl)
        {
            HttpClient client = new HttpClient();
            HttpPost postMethod = new HttpPost(new Uri("http://sendcloud.sohu.com/webapi/mail.send_template.json"));
            MultipartEntity multipartEntity = new MultipartEntity();
            postMethod.Entity = multipartEntity;
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "template_invoke_name", "superjokes_verifynotice"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "substitution_vars", "{\"to\": [\"" + email + "\"], \"sub\" : { \"%username%\" : [\"" + userName + "\"],\"%joketitle%\":[\"" + jokeTitle + "\"],\"%jokeurl%\":[\"" + jokeurl + "\"]}}"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_user", "superjokes_cn"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_key", AppConfig.SendCloudKey));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "from", "*****@*****.**"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "fromname", "超级冷笑话"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "subject", "超级冷笑话审核通知"));
            CodeScales.Http.Methods.HttpResponse response = client.Execute(postMethod);

            var repCode = response.ResponseCode;
            var repResult = EntityUtils.ToString(response.Entity);
            //LogHelper.Info("verify:" + email + repResult + AppConfig.SendCloudKey);
        }
        public void HttpPost()
        {
            HttpClient client = new HttpClient();
            HttpPost postMethod = new HttpPost(new Uri(Constants.HTTP_POST_200));
            List<NameValuePair> nameValuePairList = new List<NameValuePair>();
            nameValuePairList.Add(new NameValuePair("param1","value1"));
            nameValuePairList.Add(new NameValuePair("param2","!#$^&*((<>"));
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, Encoding.UTF8);
            postMethod.Entity = formEntity;
            HttpResponse response = client.Execute(postMethod);

            Assert.AreEqual(200, response.ResponseCode);
            string responseString = EntityUtils.ToString(response.Entity);
            MessageData md = new MessageData();
            md.PostParameters.Add(new NameValuePair("param1", "value1"));
            md.PostParameters.Add(new NameValuePair("param2", "!#$^&*((<>"));
            Assert.AreEqual(md.ToString(), responseString);
            Assert.AreEqual(Constants.HTTP_POST_200, response.RequestUri.AbsoluteUri);
            Console.Write(responseString);
        }
Example #7
0
        public void HttpPostWithParameters2()
        {
            HttpClient client = new HttpClient();

            // first request - to get cookies
            HttpGet getMethod = new HttpGet(new Uri(Constants.HTTP_POST_W3SCHOOLS));
            HttpResponse response = client.Execute(getMethod);
            Assert.AreEqual(200, response.ResponseCode);

            // second request - to send form data
            HttpPost postMethod = new HttpPost(new Uri(Constants.HTTP_POST_W3SCHOOLS));
            List<NameValuePair> nameValuePairList = new List<NameValuePair>();
            nameValuePairList.Add(new NameValuePair("fname", "user 1"));
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, Encoding.UTF8);
            postMethod.Entity = formEntity;
            response = client.Execute(postMethod);

            Assert.AreEqual(200, response.ResponseCode);
            string responseString = EntityUtils.ToString(response.Entity);
            Assert.IsTrue(responseString.Contains("Hello user 1!"));

        }
Example #8
0
        public static bool SendSMS(SendCloud sc)
        {
            try
            {
                string sVar = GetVarJson(sc.listVar[0]);

                StringBuilder sb = new StringBuilder();
                sb.Append(sc.API_Key).Append("&");
                sb.Append("phone=").Append(sc.To).Append("&");
                sb.Append("smsUser="******"&");
                sb.Append("templateId=").Append(sc.Template).Append("&");
                sb.Append("vars=").Append(sVar).Append("&");
                sb.Append(sc.API_Key);
                string sSignature = Encryption.UserMd5(sb.ToString());

                HttpClient client = new HttpClient();
                HttpPost postMethod = new HttpPost(new Uri("http://sendcloud.sohu.com/smsapi/send"));

                MultipartEntity multipartEntity = new MultipartEntity();
                postMethod.Entity = multipartEntity;

                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "smsUser", sc.API_User));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "signature", sSignature));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "templateId", sc.Template));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "phone", sc.To));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "vars", sVar));

                HttpResponse response = client.Execute(postMethod);

                if (response.ResponseCode == 200)
                {
                    string entity = EntityUtils.ToString(response.Entity);
                    Hashtable ht = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Hashtable>(entity);
                    if ((string)ht["message"] == "请求成功" && (bool)ht["result"])
                        return true;
                    else
                        return false;
                }
                else
                    return false;
            }
            catch (Exception e)
            {
                return false;
            }
        }
Example #9
0
        public static string SendEmailByList(SendCloud sc)
        {
            try
            {
                HttpClient client = new HttpClient();
                HttpPost postMethod = new HttpPost(new Uri("http://sendcloud.sohu.com/webapi/mail.send_template.json"));

                MultipartEntity multipartEntity = new MultipartEntity();
                postMethod.Entity = multipartEntity;

                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_user", sc.API_User));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_key", sc.API_Key));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "from", sc.From));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "fromname", sc.FromName));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "subject", string.Join(" ", sc.listSubject.ToArray())));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "template_invoke_name", sc.Template));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "use_maillist", "true"));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "to", sc.To));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "label", sc.Label));

                HttpResponse response = client.Execute(postMethod);

                return response.ResponseCode.ToString();
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }
Example #10
0
        public static bool SendEmail(SendCloud sc)
        {
            bool result = false;
            int iPageSize = 100;
            int iPageCount = sc.listVar.Count / iPageSize + (sc.listVar.Count % iPageSize == 0 ? 0 : 1);
            for (int i = 0; i < iPageCount; i++)
            {
                try
                {
                    string sVar = GetVarJson(sc.listVar.GetRange(i * iPageSize, i == iPageCount - 1 ? sc.listVar.Count - iPageSize * i : iPageSize));
                    sVar = sVar.Replace("\r", "").Replace("\n", "");

                    HttpClient client = new HttpClient();
                    HttpPost postMethod = new HttpPost(new Uri("http://sendcloud.sohu.com/webapi/mail.send_template.json"));

                    MultipartEntity multipartEntity = new MultipartEntity();
                    postMethod.Entity = multipartEntity;

                    multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_user", sc.API_User));
                    multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_key", sc.API_Key));
                    multipartEntity.AddBody(new StringBody(Encoding.UTF8, "from", sc.From));
                    multipartEntity.AddBody(new StringBody(Encoding.UTF8, "fromname", sc.FromName));
                    multipartEntity.AddBody(new StringBody(Encoding.UTF8, "subject", string.Join(" ", sc.listSubject.ToArray())));
                    multipartEntity.AddBody(new StringBody(Encoding.UTF8, "template_invoke_name", sc.Template));
                    multipartEntity.AddBody(new StringBody(Encoding.UTF8, "label", sc.Label));
                    multipartEntity.AddBody(new StringBody(Encoding.UTF8, "substitution_vars", sVar));

                    HttpResponse response = client.Execute(postMethod);
                    //result += sVar + Environment.NewLine;

                    if (response.ResponseCode == 200)
                        result = true;
                    //{
                    //    result += EntityUtils.ToString(response.Entity) + Environment.NewLine + "==============" + Environment.NewLine;
                    //}
                    //else
                    //    result += response.ResponseCode.ToString() + Environment.NewLine + "==============" + Environment.NewLine;
                }
                catch (Exception ex)
                {
                    //return ex.ToString();
                }
            }
            return result;
        }
Example #11
0
        internal HttpClient InitializeTransport(out MultipartEntity multipartEntity, out HttpPost postMethod)
        {
            var client = new HttpClient();
            postMethod = new HttpPost(new Uri(_restEndpoint));

            multipartEntity = new MultipartEntity();
            postMethod.Entity = multipartEntity;
            return client;
        }
        public void HttpPostWithFilesAndParameters2()
        {
            // this method sends a file and checks for the number of bytes recieved
            HttpClient client = new HttpClient();
            HttpPost postMethod = new HttpPost(new Uri(Constants.HTTP_MULTIPART_POST_200));

            MultipartEntity multipartEntity = new MultipartEntity();

            string fileName = "big-text.txt";

            FileInfo fi = ResourceManager.GetResourceFileInfo(fileName);
            FileBody fileBody1 = new FileBody("file", fileName, fi, "text/plain");

            multipartEntity.AddBody(fileBody1);
            postMethod.Entity = multipartEntity;

            StringBody stringBody1 = new StringBody(Encoding.ASCII, "param1", "value1");
            multipartEntity.AddBody(stringBody1);
            StringBody stringBody2 = new StringBody(Encoding.ASCII, "param2", "!#$^&*((<>");
            multipartEntity.AddBody(stringBody2);

            HttpResponse response = client.Execute(postMethod);

            Assert.AreEqual(200, response.ResponseCode);
            string responseString = EntityUtils.ToString(response.Entity);
            MessageData md = new MessageData();
            md.PostParameters.Add(new NameValuePair("param1", "value1"));
            md.PostParameters.Add(new NameValuePair("param2", "!#$^&*((<>"));
            md.Files.Add(new NameValuePair(fileName, fi.Length.ToString()));
            Assert.AreEqual(md.ToString(), responseString);
            Assert.AreEqual(Constants.HTTP_MULTIPART_POST_200, response.RequestUri.AbsoluteUri);
            Console.Write(responseString);
        }
        public void HttpPostWithFilesAndParameters3()
        {
            // this method sends a file and checks for the number of bytes recieved
            HttpClient client = new HttpClient();
            string url = "http://www.fiddler2.com/sandbox/FileForm.asp";
            HttpPost postMethod = new HttpPost(new Uri(url));

            MultipartEntity multipartEntity = new MultipartEntity();
            postMethod.Entity = multipartEntity;

            string fileName = "small-text.txt";

            StringBody stringBody1 = new StringBody(Encoding.ASCII, "1", "1_");
            multipartEntity.AddBody(stringBody1);

            FileInfo fi = ResourceManager.GetResourceFileInfo(fileName);
            FileBody fileBody1 = new FileBody("fileentry", fileName, fi, "text/plain");
            multipartEntity.AddBody(fileBody1);

            StringBody stringBody2 = new StringBody(Encoding.ASCII, "_charset_", "windows-1252");
            multipartEntity.AddBody(stringBody2);

            HttpResponse response = client.Execute(postMethod);

            Assert.AreEqual(200, response.ResponseCode);
            string responseString = EntityUtils.ToString(response.Entity);
            Assert.AreEqual(url, response.RequestUri.AbsoluteUri);
            Console.Write(responseString);
        }