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";
}
?>