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 ...
随机推荐
- hdu 2052
PS:竟然一次AC....惊喜...注意每个实例后跟一个空行.. 学到一个快速清空数组的用法...memst函数. memst(void *s,char a,sizeof n) 把 s里面的前n个 ...
- UIWebView stringByEvaluatingJavaScriptFromString的使用方法
stringByEvaluatingJavaScriptFromString 使用stringByEvaluatingJavaScriptFromString方法,需要等UIWebView中的页面加载 ...
- iOS 调用地图导航
在IOS6.0系统后,兼容iOS5.0与iOS6.0地图导航,需要分两个步骤 #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevic ...
- JLOI2010 冠军调查 最小割
var b,f:..] of longint; s,t,i,j,n,m,x,y:longint; l,h:..]of longint; a:..,..]of longint; procedure bf ...
- nginx源码学习资源(不断更新)
nginx源码学习是一个痛苦又快乐的过程,下面列出了一些nginx的学习资源. 首先要做的当然是下载一份nginx源码,可以从nginx官方网站下载一份最新的. 看了nginx源码,发现这是一份完全没 ...
- linux下C语言中的flock函数用法 【转】
表头文件 #include<sys/file.h> 定义函数 int flock(int fd,int operation); 函数说明 flock()会依参数operation所指 ...
- 《JavaScript Ninja》之挥舞函数
挥舞函数 匿名函数为什么如此重要 通常使用匿名函数的情况是,创建一个供以后使用的函数.例如,将匿名函数保存在一个变量里,将其作为一个对象的方法,或者是将匿名函数作为一个回调.-->在这些情况下, ...
- MapBox TileMill
TileMill 学习网站: Walkthrough: Creating tiles with Mapnik using TileMill TileMill 快速入门 Cartography With ...
- ZMMR103-数据批量导入
************************************************************************ Title : ZMMR103 ** Applicat ...
- SAP采购订单屏幕增强
转自<http://blog.csdn.net/heng0757/article/details/8073875> 为采购订单增加一个页标签,在其中放入客户自定义字段, 1. CMOD 增 ...