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(); }
相关文章推荐
- C#实现请求服务器,类似于asp下的getHTTPPage(url)功能
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- C# 实现http协议GET、POST请求
- 使用HttpURLConnection向服务器发送post和get请求
- C#实现http协议GET、POST请求
- Android中使用HttpURLConnection和HttpClient实现GET和POST请求访问网络
- 使用HttpURLConnection向服务器发送post和get请求
- C# 实现HTTP GET POST请求
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- "Android下使用HttpUrlConntion"进行最简单的get,post请求(包括创建服务器)
- 使用HttpURLConnection向服务器发送post和get请求
- C#实现http协议支持上传下载文件的GET、POST请求
- HttpURL连接远程服务器Get和Post方式请求并返回数据
- C#实现http协议支持上传下载文件的GET、POST请求
- HttpURLConnection+AsyncTask+接口回调实现简易get联网请求封装框架
- 【转】C# 实现http协议GET、POST请求
- C#实现http协议GET、POST请求 2010-10-11 16
- C#实现http协议GET、POST请求
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- 使用HttpURLConnection向服务器发送post和get请求