问题:POST请求参数含有中文,已将含中文的string以UTF-8编码格式转为byte[],并写入到请求流中,但服务器收到数据后以UTF-8解码,得到的依然是乱码!
百度到了以下方法,但依然无法解决问题:
byte[] data = Encoding.UTF8.GetBytes(buffer.ToString());
因为问题根本不在这里,而是在必须写上ContentType,并指明字符集 。
同时总结POST请求的写法。
联网工具类:
public string HttpPostRequest(string url, IDictionary<string, string> parameters)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(GlobalVariable.SERVER_ADDRESS_TEMP + url);
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=utf8";
httpWebRequest.Timeout = 20000;
if (!(parameters == null || parameters.Count == 0))
{
StringBuilder buffer = new StringBuilder();
int i = 0;
foreach (string key in parameters.Keys)
{
if (i > 0)
{
buffer.AppendFormat("&{0}={1}", key, parameters[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, parameters[key]);
}
i++;
}
byte[] data = Encoding.UTF8.GetBytes(buffer.ToString());
using (Stream stream = httpWebRequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
string responseContent = streamReader.ReadToEnd();
streamReader.Close();
httpWebResponse.Close();
httpWebRequest.Abort();
return responseContent;
}
public string HttpPostRequest(string url)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(GlobalVariable.SERVER_ADDRESS_TEMP + url);
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = "POST";
httpWebRequest.Timeout = 20000;
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
string responseContent = streamReader.ReadToEnd();
streamReader.Close();
httpWebResponse.Close();
httpWebRequest.Abort();
return responseContent;
}
调用以上方法
private void GetCityAndCommunityJsonData()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("provinceName", "广西壮族自治区");
string request = GlobalVariable.GET_CITYS_BY_PROVINCE_TO_CLIENT;
string json = NetworkUtils.Instance.HttpPostRequest(request, dic);
System.Console.WriteLine("完成:获取城市/小区数据");
list = JsonConvert.DeserializeObject<List<CityAndCommunity>>(json);
for (int i = 0; i < list.Count; i++)
{
System.Console.WriteLine(list[i].ToString());
}
}
class CityAndCommunity
{
public string City { get; set; }
public string[] Community { get; set; }
public override string ToString()
{
if (Community != null)
{
string communityStr = "";
for (int i = 0; i < Community.Length; i++)
{
if (i != Community.Length - 1)
{
communityStr += Community[i] + " , ";
}
else
{
communityStr += Community[i].ToString();
}
}
return "===========================================\nCity = "
+ City + " , communityStr = " + communityStr;
}
return base.ToString();
}
}
坑点:
-
必须加上httpWebRequest.ContentType = “application/x-www-form-urlencoded;charset=utf8”; 其中字符集也可以是gb2312,只要跟给文本数据编码采用同样格式即可。
极其重要的参考:
http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/u011185231/article/details/52090334
|
请发表评论