引入的命名空间有:
using System.IO;
using System.Net;
using System .Text;

Post方法:
ASCIIEncoding encoding=new ASCIIEncoding();
string postData="username=test";
postData += ("&password=111");
byte[] data = encoding.GetBytes(postData);

HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://www.qq.com");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Close();
HttpWebResponse myResponse=(HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(),Encoding.Default);
string result = reader.ReadToEnd();
result = result.Replace("\r","").Replace("\n","").Replace("\t","");
int status = (int)myResponse.StatusCode;
reader.Close();
 
Get方法:
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("http://www.baidu.com");
httpRequest.Timeout = 2000;
httpRequest.Method = "GET";
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
string result = sr.ReadToEnd();
result = result.Replace("\r","").Replace("\n","").Replace("\t","");
int status = (int)httpResponse.StatusCode;
sr.Close();
posted on 2011-09-06 22:33  小伟地方  阅读(15876)  评论(0编辑  收藏  举报