6

我的问题:我想用 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é(这个问题有问题)

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

4

2 回答 2

7

设置 StreamWriter 的编码:

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

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

于 2013-07-24T12:24:21.693 回答
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);

于 2013-07-24T13:14:43.573 回答