C# HttpRequest 中文编码问题
工作中的项目要用到别家的网络短信平台,工作中遇到中文编码的问题,特总结以备忘。
GET方法:
public string DoWebRequest(string url)
{
HttpWebResponse webResponse = null;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
string responseStr = null;
webRequest.Timeout = 50000;
webRequest.ContentType = "text/html; charset=gb2312";
try
{
//尝试获得要请求的URL的返回消息
webResponse = (HttpWebResponse)webRequest.GetResponse();
}
catch (WebException e)
{
//发生网络错误时,获取错误响应信息
responseStr = "发生网络错误!请稍后再试";
}
catch (Exception e)
{
//发生异常时把错误信息当作错误信息返回
responseStr = "发生错误:" + e.Message; }
finally
{
if (webResponse != null)
{
//获得网络响应流
using (StreamReader responseReader = new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("GB2312")))
{
responseStr = responseReader.ReadToEnd();//获得返回流中的内容
}
webResponse.Close();//关闭web响应流
}
}
return responseStr;
}
注意:url中的中文,要先用HttpUtility.UrlEncode("内容",编码) 用服务器接收的编码,编码一下。
POST方法:
private string DoWebRequestByPost(string url, string param)
{
HttpWebResponse webResponse = null;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
//使用post方式提交
webRequest.Method = "POST";
string responseStr = null;
webRequest.Timeout = 50000;
//要post的字节数组
byte[] postBytes = encoding.GetBytes(param);
webRequest.ContentType = "application/x-www-form-urlencoded;";
webRequest.ContentLength = postBytes.Length; using (Stream reqStream = webRequest.GetRequestStream())
{
reqStream.Write(postBytes, 0, postBytes.Length);
}
try
{
//尝试获得要请求的URL的返回消息
webResponse = (HttpWebResponse)webRequest.GetResponse();
}
catch (Exception)
{
//出错后直接抛出
throw;
}
finally
{
if (webResponse != null)
{
//获得网络响应流
using (StreamReader responseReader = new StreamReader(webResponse.GetResponseStream(), encoding))
{
responseStr = responseReader.ReadToEnd();//获得返回流中的内容
}
webResponse.Close();//关闭web响应流
}
}
return responseStr;
}
encoding为服务器接收的编码,例如:Encoding.GetEncoding("GBK")等
param post请求的参数 param1=123¶m2=中国¶m3=abc 这样的格式,中文部分不用使用编码,方法内转成byte[]时
会进行编码。
C# HttpRequest 中文编码问题的更多相关文章
- HttpRequest 类
关于此类的介绍:查看HttpRequest类 点击查看:HttpRequest中方法的封装 跟这个类对应的HttpResponse类 定义:使 ASP.NET 能够读取客户端在 Web 请求期间发送的 ...
- 难道.NET Core到R2连中文编码都不支持吗?
今天写了一个简单的.NET Core RC2控制台程序,发现中文显示一直是乱码.查看操作系统设置,没有问题:查看源文件编码,也没有问题:甚至查看了Console字符编码相关的注册表,依然没有发现问题. ...
- Java Web中的中文编码
Java Web开发中经常会遇到中文编码问题,那么为什么需要编码呢?因为人类需要表示的符号太多,无法用1个字节来表示,而计算机中存储信息最小单元为1个字节.所以必须指定char与byte之间的编码规则 ...
- Java页面中文编码要转换两次encodeURI
1.js文件中使用encodeURI()方法. login_name = encodeURI(encodeURI(login_name)); 2.action中URLDecoder解码 loginNa ...
- .net学习笔记----HttpRequest,WebRequest,HttpWebRequest区别
WebRequest是一个虚类/基类,HttpWebRequest是WebRequest的具体实现 HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所 ...
- 防刷票机制研究和.NET HttpRequest Proxy
最近应朋友之约 测试他做的投票网站 防刷票机制能力如何,下面有一些心得和体会. 朋友网站用PHP写的,走的是HttpRequest,他一开始认为IP认证应该就差不多了.但说实话这种很low,手动更换代 ...
- ZKUI中文编码以及以docker方式运行的问题
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- python httprequest, locust
r = self.client.get("/orders", headers = {"Cookie": self.get_user_cookie(user[0] ...
- R语言读写中文编码方式
最近遇到一个很头疼的事,就是 R语言读写中文编码方式.在网上找到了一篇博文,谢谢博主的精彩分享,让我很快解决了问题,在此也分享一下 R语言读写数据的方法很多,这里主要是我在使用read.csv/rea ...
随机推荐
- WPF制作子窗体的弹出动画效果
创建一个WPF应用程序WpfApplication1,新建个窗体DialogWin <Windowx:Class="WpfApplication1.DialogWin" xm ...
- hdu 2058
PS:TLE了N次...虽然结果对了...后来看了公式才知道要枚举项数才行... 代码: #include "stdio.h"#include "math.h" ...
- <xliff:g>标签
摘要: 这是Android4.3Mms源码中的strings.xml的一段代码: <!--Settings item desciption for integer auto-delete sms ...
- 评价软件_搜狗输入法(pc端)
一.用户界面 用户在初次下载后,会有一个非常简洁的界面,如图:
- js中的apply调用
今天看了阮一锋老师的一篇文章,感觉很明了对闭包的理解,尤其是文章中的apply的介绍 apply()是函数对象的一个方法,它的作用是改变函数的调用对象,它的第一个参数就表示改变后的调用这个函数的对象. ...
- 手机端APP原型相关
http://www.isux.us/demoo/index.php(很慢) https://www.mockplus.cn/
- shell MAC 地址 校验
/*************************************************************************************** * shell MAC ...
- CSS 实现:图片+文字的布局(综合)
☊[实现要求]:图片+文字+居中 √[实现]: ① img + 文字 <div class="demo2-1"> <img src="" al ...
- python 的 class
虽然python OOP 是可选的,但是尽可能使用OOP, 可以让你的代码更精简,容易理解. python 中大多数OOP故事, 都可简化为这个表达式: object.attribute 类的建立使用 ...
- C++ 中的C_str()函数用法
语法: const char *c_str(); c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同. 这是为了与c语言兼容,在c语言中没有string类型,故必须通过 ...