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 ...
随机推荐
- java中判断字符串是否为数字的三种方法
以下内容引自 http://www.blogjava.net/Javaphua/archive/2007/06/05/122131.html 1用JAVA自带的函数 public static ...
- [转]dev C++编写windows程序遇到问题
1.工具-编译选项-编译器-在连接器命令行加入以下命令: -mwindows 2.出现错误:undefined reference to `PlaySoundA@12' 解决办法:工具-编译选项-编译 ...
- struts2和servlet同时用(访问servlet时被struts2过滤器拦截问题的解决)
在同一个项目中间,如果既用到servlet有用了struts2的框架,运行项目时可能无法正常使用servlet,原因是在配置struts2的核心控制器时<url-pattern>/*< ...
- Windows10中无法打开这个应用的解决方案
报错的图:
- Cisco ASA端口映射
Cisco ASA 端口映射设置 1.使用ASDM进入到配置页面,点开NAT Rules,然后新增Network Object,NAT选项如下图所示设定. 下图设定外网IP映射到内网IP地址192.1 ...
- document操作
1.windows对象操作 事件两个参数sender:代表事件源,即谁触发的事件e:代表事件数据load(sender,e)事件是一个特殊的委托(代理)2.document对象操作找元素:1.根据id ...
- PHP 的snmp各个版本的Wrapper封装类
原文来自:http://blog.chinaunix.net/uid-7654720-id-3211234.html class SNMP_Wrapper { protected $_host; pr ...
- Hooks
function getValType(elem) { var ret = elem.tagName.toLowerCase() return ret === "input" &a ...
- PAT (Basic Level) Practise:1004. 成绩排名
[题目链接] 读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为 第1行:正整数n 第2行:第1个学生的姓名 学号 成绩 第3 ...
- /cdrom /media /mnt
/media : Mount point for removeable media Purpose This directory contains subdirectories which are u ...