向Url发送post请求传递参数

简介:
        #region  向Url发送post请求,返回网站响应内容
        /// <summary>
        /// 向Url发送post请求,返回网站响应内容
        /// </summary>
        /// <param name="postData">发送数据</param>
        /// <param name="uriStr">接受数据的Url</param>
        /// <param name="action">更新操作</param>
        /// <returns>返回网站响应内容</returns>
        public static string RequestPost(string postData, string uriStr, string action)
        {
            HttpWebRequest requestScore = (HttpWebRequest)WebRequest.Create(uriStr);
            StringBuilder postContent = new StringBuilder();
            Encoding myEncoding = Encoding.GetEncoding("gb2312");
            postContent.Append(HttpUtility.UrlEncode(" message", myEncoding));
            postContent.Append("=");
            postContent.Append(HttpUtility.UrlEncode(postData, myEncoding));
            postContent.Append("&");
            postContent.Append(HttpUtility.UrlEncode(" type", myEncoding));
            postContent.Append("=");
            postContent.Append(HttpUtility.UrlEncode(" sync", myEncoding));
            postContent.Append("&");
            postContent.Append(HttpUtility.UrlEncode(" action", myEncoding));
            postContent.Append("=");
            postContent.Append(HttpUtility.UrlEncode(action, myEncoding));


            byte[] data = Encoding.ASCII.GetBytes(postContent.ToString());
            requestScore.Method = "Post";
            requestScore.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
            requestScore.ContentLength = data.Length;
            requestScore.KeepAlive = true;
            Stream stream = requestScore.GetRequestStream();
            stream.Write(data, 0, data.Length);
            stream.Close();
            HttpWebResponse responseSorce;
            try
            {
                responseSorce = (HttpWebResponse)requestScore.GetResponse();
            }
            catch (WebException ex)
            {
                responseSorce = (HttpWebResponse)ex.Response;//得到请求网站的详细错误提示
            }
            StreamReader reader = new StreamReader(responseSorce.GetResponseStream(), Encoding.UTF8);
            string content = reader.ReadToEnd();
            requestScore.Abort();
            responseSorce.Close();
            responseSorce.Close();
            reader.Dispose();
            stream.Dispose();
            return content;
        }

        #endregion


网站接收数据:

            string message = Request.Form["message"];
            string type = Request.Form["type"];
            string action = Request.Form["action"];

目录
相关文章
动态URL构建与HTTP请求的Kotlin实现
动态URL构建与HTTP请求的Kotlin实现
计算机网络常见面试题(二):浏览器中输入URL返回页面过程、HTTP协议特点,GET、POST的区别,Cookie与Session
计算机网络常见面试题(二):浏览器中输入URL返回页面过程、HTTP协议特点、状态码、报文格式,GET、POST的区别,DNS的解析过程、数字证书、Cookie与Session,对称加密和非对称加密
前端JS截取url上的参数
文章介绍了两种前端JS获取URL参数的方法:手动截取封装和使用URLSearchParams。
105 0
(六)网络编程之化身一个请求感受浏览器输入URL后奇妙的网络之旅!
在浏览器上输入一个URL后发生了什么? 这也是面试中老生常谈的话题,包括网上也有大量关于这块的内容。
205 2
Chrome插件实现问题之网络进程接收到URL请求后会如何解决
Chrome插件实现问题之网络进程接收到URL请求后会如何解决
【Azure 应用程序见解】Application Insights Java Agent 3.1.0的使用实验,通过修改单个URL的采样率来减少请求及依赖项的数据采集
【Azure 应用程序见解】Application Insights Java Agent 3.1.0的使用实验,通过修改单个URL的采样率来减少请求及依赖项的数据采集
【API管理 APIM】APIM中如何配置使用URL路径的方式传递参数(如由test.htm?name=xxx 变为test\xxx)
【API管理 APIM】APIM中如何配置使用URL路径的方式传递参数(如由test.htm?name=xxx 变为test\xxx)
Asp.net Webapi 的 Post 方法不能把参数加到 URL 中?试试这样写
Asp.net Webapi 的 Post 方法不能把参数加到 URL 中?试试这样写
107 0
|
8月前
|
JAVA 获取 URL 指定参数的值
JAVA 获取 URL 指定参数的值
80 0
HTTP请求流程概览:浏览器构建请求行含方法、URL和版本;检查缓存;解析IP与端口
【6月更文挑战第23天】 HTTP请求流程概览:浏览器构建请求行含方法、URL和版本;检查缓存;解析IP与端口;TCP连接(HTTP/1.1可能需排队);三次握手;发送请求头与体;服务器处理并返回响应;TCP连接可能关闭或保持;浏览器接收并显示响应,更新缓存。HTTP版本间有差异。
164 5

热门文章

最新文章