(转)C# wnform 请求http ( get , post 两种方式 )
2016-07-07 10:04
537 查看
本文转载自:http://www.cnblogs.com/hailexuexi/archive/2011/03/04/1970926.html
1.Get请求
2.Post请求
注:System.Web.HttpUtility.UrlEncode("多月"); 需要引用 System.web.dll
WinformSubmit.php 代码如下:
1.Get请求
string strURL = "http://localhost/WinformSubmit.php?tel=11111&name=张三"; System.Net.HttpWebRequest request; // 创建一个HTTP请求 request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); //request.Method="get"; System.Net.HttpWebResponse response; response = (System.Net.HttpWebResponse)request.GetResponse(); System.IO.StreamReader myreader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8); string responseText = myreader.ReadToEnd(); myreader.Close(); MessageBox.Show(responseText);
2.Post请求
string strURL = "http://localhost/WinformSubmit.php"; System.Net.HttpWebRequest request; request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); //Post请求方式 request.Method = "POST"; // 内容类型 request.ContentType = "application/x-www-form-urlencoded"; // 参数经过URL编码 string paraUrlCoded = System.Web.HttpUtility.UrlEncode("keyword"); paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode("多月"); byte[] payload; //将URL编码后的字符串转化为字节 payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); //设置请求的 ContentLength request.ContentLength = payload.Length; //获得请 求流 System.IO.Stream writer = request.GetRequestStream(); //将请求参数写入流 writer.Write(payload, 0, payload.Length); // 关闭请求流 writer.Close(); System.Net.HttpWebResponse response; // 获得响应流 response = (System.Net.HttpWebResponse)request.GetResponse(); System.IO.StreamReader myreader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8); string responseText = myreader.ReadToEnd(); myreader.Close(); MessageBox.Show(responseText);
注:System.Web.HttpUtility.UrlEncode("多月"); 需要引用 System.web.dll
WinformSubmit.php 代码如下:
<?php header("content-Type: text/html; charset=Utf-8"); echo mb_convert_encoding("123abc娃哈哈", "UTF-8", "GBK"); echo "\n------\n"; foreach($_POST as $key => $value){ echo $key . '--' .$value ."\n"; } echo "\n-------\n"; foreach($_GET as $key => $value){ echo $key . '--' .$value ."\n"; } ?>
相关文章推荐
- 各种工具使用手册:http://www.itshouce.com.cn/linux/linux-tcpdump.html 关于tcpdump!!!!
- HTML <meta> 标签 遇到<meta http-equiv="refresh" content="0; url="&glt;详解
- 移植完linux-3.4.2内核,启动系统后使用命令ifconfig -a查看网络配置只有一个local backloop
- 如何在 Ubuntu 15.04/CentOS 7 中安装 Lighttpd Web 服务器
- 电脑(windows)重启/启动后发现网络联接不上的解决办法
- springMVC--关于no mapping found for http request with uri
- 原生JS发起http请求,并且接收返回值
- 网络获取图片轮播
- 网络视频直播简介
- Linux下C++实现http请求
- zabbix监控tcp连接数
- iOS重构——轻量级的网络请求封装实践
- HttpUlity
- HttpUtil
- linux串口转TCP程序
- 卸载趋势防火墙网络版
- Linux C: 基于C/S的多线程网络编程 2 (多客户端)
- Linux C: 基于C/S的多线程网络编程 (服务器充当一个客户端的两点传输)
- Android Volley框架的使用(四)图片的三级缓存策略(内存LruCache+磁盘DiskLruCache+网络Volley)
- HTTPS 是如何保证安全的?