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



我的问题:我想用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();


地址。Ville = Fercé (这个 é 有问题(

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

设置 StreamWriter 的编码:

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

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

将你的代码替换为:

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);

相关内容

  • 没有找到相关文章

最新更新