如何用StreamWriter写特殊字符(é,è)?

时间:2013-07-24 11:20:36

标签: c# asp.net special-characters streamwriter.write

我的问题:我想用StreamWriter写我的数据(我的字符串包含字母é和è)但它不起作用。没有这些字母,它就可以了。

错误:无法关闭流

我的代码:

 string postString = "id=" + sIdTransaction + "&nom=" + sNom + "&prenom=" + sPrenom + "&email=" + sEmail + "&adresse.rue=" + sRue + "&adresse.codePostal=" + sCodePostal + "&adresse.ville=" + sVille;
string sIdContractant = "";
Encoding iso = Encoding.GetEncoding("utf-8");
Encoding utf8 = Encoding.UTF8;
byte[] utfBytes = utf8.GetBytes(postString);
byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
postString = iso.GetString(isoBytes);

string sUrlAuth = "https://test.contralia.fr/Contralia/api/transactions/" + sIdTransaction + "/contractant/";
HttpWebRequest webRequest = CreationRequete(sUrlAuth);
webRequest.ContentLength = postString.Length;


StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
requestWriter.Write(postString);
requestWriter.Close();


adresse.Ville =Fercé(这个é的问题)

我该如何解决这个问题?
感谢

2 个答案:

答案 0 :(得分:6)

设置StreamWriter的编码:

StreamWriter sw = new StreamWriter(webRequest.GetRequestStream(), encoding)

试试这个,我希望它有所帮助。它对我有用!

答案 1 :(得分:1)

用以下代码替换您的代码:

string postString = "id=" + sIdTransaction + "&nom=" + sNom + "&prenom=" + sPrenom + "&email=" + sEmail + "&adresse.rue=" + sRue + "&adresse.codePostal=" + sCodePostal + "&adresse.ville=" + sVille;
string sIdContractant = "";
Encoding iso = Encoding.GetEncoding("utf-8");
Encoding utf8 = Encoding.UTF8;
byte[] utfBytes = utf8.GetBytes(postString);
byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
postString = iso.GetString(isoBytes);

string sUrlAuth = "https://test.contralia.fr/Contralia/api/transactions/" + sIdTransaction + "/contractant/";
HttpWebRequest webRequest = CreationRequete(sUrlAuth);
webRequest.ContentLength = postString.Length;


StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream(), iso);
requestWriter.Write(postString);
requestWriter.Close();

我只是换了你的第11行,StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
由于StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream(), iso);