您的位置:首页 > 理论基础 > 计算机网络

C#实现请求服务器,类似于asp下的getHTTPPage(url)功能

2017-07-10 14:12 459 查看
编写一个类,需要添加一些引用

using System.Net;
using System.Text;
using System.IO;


代码如下

public static CookieContainer Cook = new CookieContainer();
public static string CreatRequestGetResponse(string URL, string PostData)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
myRequest.CookieContainer = Cook;
myRequest.KeepAlive = true;
myRequest.Method = "POST";
string postdata = PostData;
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
byte[] postarr = encode.GetBytes(postdata);
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = postarr.Length;
Stream outStream = myRequest.GetRequestStream();
outStream.Write(postarr, 0, postarr.Length);
outStream.Close();
WebResponse myResponse = myRequest.GetResponse();
StreamReader SReader = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
return SReader.ReadToEnd();
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐