Frage

My problem : I want to write datas with a StreamWriter (my String contains letters é and è) but it doesn't work. Without this letters, it works.

The error : Cannot close the stream

My code :

 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é (problem with this é)

How can I resolve this problem please ?
Thanks

War es hilfreich?

Lösung

Set the encoding for the StreamWriter:

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

Try this, I hope it helps. It worked for me!

Andere Tipps

Replace your code by this :

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

i just changer, in your 11th line, StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
by
StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream(), iso);

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top