码迷,mamicode.com
首页 > 其他好文 > 详细

发送post请求

时间:2016-06-20 18:50:21      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 发送post请求
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSendPost_Click(object sender, EventArgs e)
        {
            test1();
        }
        private void test1()
        {
            string strUrl = @"http://localhost:16542/Manage/usercenter/data/zhibo.ashx";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            //dic.Add("k", @"Q0tRbDZXbkJyektOTDlua1RjR3lJL0VDVTR0R0w3MytwdWRvUU80V2t0THpxbWo0TnZhMVhNakQ5aHlaQ296bm55R2s2Ym5yOWlKZTQ3VEtwZ2JGZHc9PQ==");
            dic.Add("k", "{\"SDKID\":\"mddZx2FZqr\",\"TsTime\":\"1466407575\",\"UPID\":\"1\"}");
            CreatePostHttpResponse(strUrl, dic, System.Text.Encoding.UTF8);
        }
        private  void test2()
        {
            string strUrl = @"http://localhost:9505/Home/checkUser2";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("k", @"d1o1Q3Jab29KWHJWenh3ZmFiSnM1WkhTZGxDRm5SOHcrVHVxTm0vOXROWjBhWkFxa2xhNmRQWVhmUlNqRHZQUUw4bjlyWXMvSnB6TXRDeWtqTWVveDQzWEZtbUg0VTQyK1Y5Y2pWWGdBOTQ9");
            CreatePostHttpResponse(strUrl, dic, System.Text.Encoding.UTF8);
        }
        private  void test3()
        {
            string strUrl = @"http://localhost:9505/Home/checkUser3";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("k", "{\"SDKID\":\"mddZx2FZqr\",\"TsTime\":\"1466407575\",\"UPID\":\"1\"}");
            CreatePostHttpResponse(strUrl, dic, System.Text.Encoding.UTF8);
        }

        /// <summary>
        /// 发送post请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="parameters"></param>
        /// <param name="charset"></param>
        /// <returns></returns>
        public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, Encoding charset)
        {
            HttpWebRequest request = null;
            //HTTPSQ请求  
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            request = WebRequest.Create(url) as HttpWebRequest;
            request.ProtocolVersion = HttpVersion.Version10;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = DefaultUserAgent;
            //如果需要POST数据     
            if (!(parameters == null || parameters.Count == 0))
            {
                StringBuilder buffer = new StringBuilder();
                int i = 0;
                foreach (string key in parameters.Keys)
                {
                    if (i > 0)
                    {
                        buffer.AppendFormat("&{0}={1}", key, parameters[key]);
                    }
                    else
                    {
                        buffer.AppendFormat("{0}={1}", key, parameters[key]);
                    }
                    i++;
                }
                byte[] data = charset.GetBytes(buffer.ToString());
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
            }
            return request.GetResponse() as HttpWebResponse;
        }
        private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true; //总是接受     
        }
    }
}

  

发送post请求

标签:

原文地址:http://www.cnblogs.com/caohuimingfa/p/5601190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!