Charset issue for French accent (WebRequest)
When I send this webrequest with a string which contains special char like "é" in queryParam["message"] = sMessage;
to the graph API it will publish on the facebook users wall '%u00e9'.
I tested with utf8 and also ISO-8859-1...
Here is my code:
NameValueCollection queryString = System.Web.HttpUtility.ParseQueryString(string.Empty);
queryString["access_token"] = facebookUser.accessToken;
string url = "https://graph.facebook.com/" + facebookUser.id + "/feed?" + queryString;
NameValueCollection queryParam = System.Web.HttpUtility.ParseQueryString(string.Empty);
queryParam["name"] = name;
queryParam["link"] = link;
queryParam["picture"] = picture;
queryParam["description"] = description;
queryParam["source"] = source;
queryParam["caption"] = caption;
queryParam["actions"] = action;
queryParam["message"] = sMessage;
var webRequest = WebRequest.Create(url);
webRequest.ContentType = "text/html; charset=ISO-8859-1";
webRequest.Method = "POST";
//webRequest.Headers.Add(HttpRequestHeader.AcceptCharset, "utf-8");
webRequest.Headers.Add("Accept-Language", "en-us,en;");
webRequest.Headers.Add(System.Net.HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8");
byte[] bytes = Encoding.UTF8.GetBytes(queryParam.ToString());
webRequest.ContentLength = bytes.Length;
System.IO.Stream os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();
try
{
var webResponse = webRequest.GetResponse();
StreamReader sr = null;
try
{
sr = new StreamReader(webResponse.GetResponseStream());
}
fi开发者_C百科nally
{
if (sr != null)
{
sr.Close();
}
}
}
Do you have an idea?
You are setting the charset to ISO-8859-1
and then you are using Encoding.UTF8.GetBytes()
. You should use Encoding.GetEncoding("ISO-8859-1").GetBytes()
or (probably better) ALWAYS USE UTF-8!
I see you had the AcceptCharset
setted to UTF-8 and then commented. But you forgot of the ContentType
.
精彩评论