C#发送Get、Post请求(带参数)
程序员文章站
2022-03-07 22:13:25
目录get请求post请求get请求1.简单发送get请求/// /// 指定url地址使用get 方式获取全部字符串/// /// &l...
get请求
1.简单发送get请求
/// <summary> /// 指定url地址使用get 方式获取全部字符串 /// </summary> /// <param name="url">请求链接地址</param> /// <returns></returns> public static string get(string url) { string result = ""; httpwebrequest req = (httpwebrequest)webrequest.create(url); httpwebresponse resp = (httpwebresponse)req.getresponse(); stream stream = resp.getresponsestream(); try { //获取内容 using (streamreader reader = new streamreader(stream)) { result = reader.readtoend(); } } finally { stream.close(); } return result; }
2.带请求参数的get方法
/// <summary> /// 发送get请求 /// </summary> /// <param name="url">地址</param> /// <param name="dic">请求参数定义</param> /// <returns></returns> public static string get(string url, dictionary<string, string> dic) { string result = ""; stringbuilder builder = new stringbuilder(); builder.append(url); if (dic.count > 0) { builder.append("?"); int i = 0; foreach (var item in dic) { if (i > 0) builder.append("&"); builder.appendformat("{0}={1}", item.key, item.value); i++; } } httpwebrequest req = (httpwebrequest)webrequest.create(builder.tostring()); //添加参数 httpwebresponse resp = (httpwebresponse)req.getresponse(); stream stream = resp.getresponsestream(); try { //获取内容 using (streamreader reader = new streamreader(stream)) { result = reader.readtoend(); } } finally { stream.close(); } return result; }
3.自定义指定http请求头,自定义指定编码解析返回结果
httpwebrequest req = (httpwebrequest)webrequest.create(url); req.method = "get"; req.headers["accept-language"] = "zh-cn,zh;q=0.8"; req.referer = "https://www.baidu.com/"; httpwebresponse resp = (httpwebresponse)req.getresponse(); stream stream = resp.getresponsestream(); string result = ""; //注意,此处使用的编码是:gb2312 //using (streamreader reader = new streamreader(stream, encoding.default)) using (streamreader reader = new streamreader(stream, encoding.getencoding("gb2312"))) { result = reader.readtoend(); }
post请求
此处内容传输都是用utf-8编码
1.不带参数发送post请求
/// <summary> /// 指定post地址使用get 方式获取全部字符串 /// </summary> /// <param name="url">请求后台地址</param> /// <returns></returns> public static string post(string url) { string result = ""; httpwebrequest req = (httpwebrequest)webrequest.create(url); req.method = "post"; httpwebresponse resp = (httpwebresponse)req.getresponse(); stream stream = resp.getresponsestream(); //获取内容 using (streamreader reader = new streamreader(stream, encoding.utf8)) { result = reader.readtoend(); } return result; }
2.带参数post请求,指定键值对
/// <summary> /// 指定post地址使用get 方式获取全部字符串 /// </summary> /// <param name="url">请求后台地址</param> /// <returns></returns> public static string post(string url,dictionary<string,string> dic) { string result = ""; httpwebrequest req = (httpwebrequest)webrequest.create(url); req.method = "post"; req.contenttype = "application/x-www-form-urlencoded"; #region 添加post 参数 stringbuilder builder = new stringbuilder(); int i = 0; foreach (var item in dic) { if (i > 0) builder.append("&"); builder.appendformat("{0}={1}", item.key, item.value); i++; } byte[] data = encoding.utf8.getbytes(builder.tostring()); req.contentlength = data.length; using (stream reqstream = req.getrequeststream()) { reqstream.write(data, 0, data.length); reqstream.close(); } #endregion httpwebresponse resp = (httpwebresponse)req.getresponse(); stream stream = resp.getresponsestream(); //获取响应内容 using (streamreader reader = new streamreader(stream, encoding.utf8)) { result = reader.readtoend(); } return result; }
3.带参数的post请求,指定发送字符串内容
/// <summary> /// 指定post地址使用get 方式获取全部字符串 /// </summary> /// <param name="url">请求后台地址</param> /// <param name="content">post提交数据内容(utf-8编码的)</param> /// <returns></returns> public static string post(string url, string content) { string result = ""; httpwebrequest req = (httpwebrequest)webrequest.create(url); req.method = "post"; req.contenttype = "application/x-www-form-urlencoded"; #region 添加post 参数 byte[] data = encoding.utf8.getbytes(content); req.contentlength = data.length; using (stream reqstream = req.getrequeststream()) { reqstream.write(data, 0, data.length); reqstream.close(); } #endregion httpwebresponse resp = (httpwebresponse)req.getresponse(); stream stream = resp.getresponsestream(); //获取响应内容 using (streamreader reader = new streamreader(stream, encoding.utf8)) { result = reader.readtoend(); } return result; }
转载方法:
httpwebrequest req = (httpwebrequest)httpwebrequest.create("http://fanyi.baidu.com/transcontent"); encoding encoding = encoding.utf8; string param = "ie=utf-8&source=txt&query=hello&t=1327829764203&token=8a7dcbacb3ed72cad9f3fb079809a127&from=auto&to=auto"; //encoding.getbytes(postdata); byte[] bs = encoding.ascii.getbytes(param); string responsedata = string.empty; req.method = "post"; req.contenttype = "application/x-www-form-urlencoded"; req.contentlength = bs.length; using (stream reqstream = req.getrequeststream()) { reqstream.write(bs, 0, bs.length); reqstream.close(); } using (httpwebresponse response = (httpwebresponse)req.getresponse()) { using (streamreader reader = new streamreader(response.getresponsestream(),encoding)) { responsedata = reader.readtoend().tostring(); } context.response.write(responsedata); }
到此这篇关于c#发送get、post请求(带参数)的文章就介绍到这了,更多相关c#发送get、post请求内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!