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 ...
随机推荐
- python随笔
1. 使用iter实现接收用户多行输入 stopword = '' str = '' print('请将要添加的内容输入下方,输入空白行按回车退出程序:') for line in iter(inpu ...
- apache环境Zf2要装的intl验证
sudo apt-get install php5-intl
- 程序员是怎么炼成的---OC题集--练习答案与题目(2)
1.objective-c中有垃圾回收机制吗? 没有像java.C#一样的垃圾回收机制,但是有ARC自动引用计数器技术,根据对象的引用技术来判断对象是否还在使用,如果RC=0,则系统就会销毁对象,实 ...
- codeforces 580C Kefa and Park(DFS)
题目链接:http://codeforces.com/contest/580/problem/C #include<cstdio> #include<vector> #incl ...
- 解决input之间的空隙
<!doctype html> <html> <head> <meta charset="UTF-8"> <meta name ...
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
Android应用的开发过程中需要把繁重的任务(IO,网络连接等)放到其他线程中异步执行,达到不阻塞UI的效果. 下面将由浅入深介绍Android进行异步处理的实现方法和系统底层的实现原理. 本文介绍 ...
- MySQL索引建立与删除
#添加索引alter table 表名 add index 索引名称(列名1, 列名2);alter table 表名 add index 索引名称(列名1, 列名2, 列名3);alter tabl ...
- Word Ladder II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- Ubuntu14.04安装配置Open vSwitch
最近在研究学习Open vSwitch,准备在本地Ubuntu14.04机器上安装一下,并基于其创建一些KVM虚拟机,下面就简要记录下在Ubuntu14.04系统上安装Open vSwitch的过程. ...
- 使用容器控制器控制另外两个控制器的view交换
建三个UIViewController 的子控制器,其中一个为根控制器,另外两个控制器的视图作为切换对象 AppDelegate中代码 //AppDelegate.h中代码 #import <U ...