I have a little problem with a string in C#. Actually, I take a JSON flow by an URL.
WebClient webC = new WebClient();
string jsonStr = webC.DownloadString("http://www.express-board.fr/api/jobs");
But when I write the string in the console, I have the problem of encoding.
[...]"contract":"Freelance/Indépendant"[...]
I have try to used lot of trick seen on stackoverflow with Encoding class. But impossible, to solve the problem. Of course if I use the link directly in my web browser and open it in Notepadd++ no problem.
Sometimes, with some combinaison of encoding ( ACSII-> UTF-8 I think), I obtain this :
[...]"contract":"Freelance/Indépendant"[...] to
[...]"contract":"Freelance/Ind??pendant"[...]
This actually returns the string as intended:
WebClient webC = new WebClient();
webC.Encoding = Encoding.UTF8;
string jsonStr = webC.DownloadString("http://www.express-board.fr/api/jobs");
Related
Got a problem here... If I put the XML file on the server, then I can read it through steamReader, convert to variable and got everything working in the MSSQL database.
However, it is required that I send through html POST, and it doesn't work for the code below:
page.Response.ContentType = "text/xml";
StreamReader reader = new StreamReader(page.Request.InputStream);
inputString = reader.ReadToEnd();
deleteShip(inputString);
it seems to me that the above code didn't get the XML that POST from my program. Because for the same code in deleteShip, if I use an xml on the server then it works fine.
Is there a way to solve this problem? As long as I can send any string to deleteShip(string s) then I'm happy. The string will be in XML format though
Thanks for the help!
It would be useful to see how the XML is POSTed to your program. Typically, data is sent from an HTML form as name-value pairs in the HTTP request body when using the POST method. It's not clear from your question whether you're using an HTML form to POST the XML to your program and it's hard to tell what might be going wrong without more information.
From your code it looks like you're reading the entire HTTP request where you'd usually read the value of a request parameter for example:
Request["XmlParameterName"]
Where XmlParameterName is the name of an HTML form input field.
Have you inspected the value of the inputString variable? Is it valid XML? Is it encoded correctly? Are any invalid characters like ampersands (&) escaped correctly?
Update your question with a bit more information if none of the things I mentioned are the problem.
OK, I got it fixed.
Here is the code.
System.IO.Stream stream;
string inputString;
Int32 stringLength;
stream = Request.InputStream;
stringLength = Convert.ToInt32(stream.Length);
byte[] stringArray = new byte[stringLength];
inputString = System.Text.Encoding.ASCII.GetString(stringArray, 0, stringLength);
deleteShip(inputString);
By this it will access the POST body from my html request (which in this case XML).
The Facebook graph API's return to me the user's email address as
foo\u0040bar.com.
in a JSON object. I need to convert it to
foo#bar.com.
There must be a built in method in .NET that changes the Unicode character expression (\u1234) to the actual unicode symbol.
Do you know what it is?
Note: I prefer not to use JSON.NET or JavaScriptSerializer for performance issues.
I think the problem is in my StreamReader:
requestUrl = "https://graph.facebook.com/me?access_token=" + accessToken;
request = WebRequest.Create(requestUrl) as HttpWebRequest;
try
{
using (HttpWebResponse response2 = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
reader = new StreamReader(response2.GetResponseStream(),System.Text.Encoding.UTF8);
string json = reader.ReadToEnd();
I tried different encodings for the StreamReader, UTF8, UTF7, Unicode, ... none worked.
Many thanks!
Thanks to L.B for correcting me. The problem was not in the StreamReader.
Yes, there is some built in method for that, but that would involve something like using a compiler to parse the string as code...
Use a simple replace:
s = s.Replace(#"\u0040", "#");
For a more flexible solution, you can use a regular expression that can handle any unicode character:
s = Regex.Replace(s, #"\\u([\dA-Fa-f]{4})", v => ((char)Convert.ToInt32(v.Groups[1].Value, 16)).ToString());
Json responses are not binary data to convert to a string using some encodings. Instead they are strings correctly decoded by your browser or by HttpWebResponse as in your example. You need a second procesing on it(regex, deserializers etc) to get the final data.
See what you get with
webClient.DownloadString("https://graph.facebook.com/HavelVaclav?access_token=????") without any encoding
{"id":"100000042150992",
"name":"Havel V\u00e1clav",
"first_name":"Havel",
"last_name":"V\u00e1clav",
"link":"http:\/\/www.facebook.com\/havel.vaclav",
"username":"havel.vaclav",
"gender":"male",
"locale":"cs_CZ"
}
Would your encoding change \/ to /?
So, the problem is not in your StreamReader.
I have a PHP SOAP server (using nuSOAP with wsdl) that send the content of a html page. Of course, the HTML can be coded with differents encoding and here is when the problems appear. If I used a PHP SOAP client I can send the encoding like this:
$clienteSOAP = new SoapClient ("http://test.mine.com/wsdl/filemanager?wsdl",
array ('encoding' => 'ISO-8859-15'));
$clienteSOAP->__soapCall ("Test.uploadHTML",
array (file_get_contents ('/home/КОЛЛЕКЦИЯ_РОДНИК_ПРЕМИУМ.html')));
And if I put the correct encoding, has never failed so far. But when I use a C# client, how can I put the encoding in the web service petition? In C# the code is:
System.IO.StreamReader html = new System.IO.StreamReader (
"C:\\Documents and Settings\\КОЛЛЕКЦИЯ_РОДНИК_ПРЕМИУМ.html"
,System.Text.Encoding.GetEncoding("iso-8859-15"));
string contenido = html.ReadToEnd();
html.Close();
Test.FileManager upload = new Test.FileManager();
string resultado = upload.TestUploadHTML (contenido);
Test.FileManager is a Web reference of the wsdl, and when I see the "upload html" some characters aren't correct.
Thanks in advance.
nusoap internally uses the php function xml_parser_create, that only supports: ISO-8859-1, UTF-8 and US-ASCII. For this reason, this library don't works well with other encoding. Great PacHecoPe...
UPDATE: The best option, in my case, is read the archive in its original encoding and transform it to utf-8:
System.IO.StreamReader html = new System.IO.StreamReader (
"C:\\Documents and Settings\\КОЛЛЕКЦИЯ_РОДНИК_ПРЕМИУМ.html"
,System.Text.Encoding.GetEncoding("iso-8859-15"));
string contenido = html.ReadToEnd();
html.Close();
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes (contenido);
string contenidoUTF8 = encoder.GetString(bytes);
upload.RequestEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Test.FileManager upload = new Test.FileManager();
string resultado = upload.TestUploadHTML (contenidoUTF8);
UPDATE2: With encoding that not supported in UTF-8 like big5, don't work very well the above code. For this reason, it's better don't make the transform to UTF-8 and set the parameter with the content of html like base64Binary, in the wsdl.
I am trying to use System.windows.Forms.WebBrowser to display a content in the languages other than English, but the resulting encoding is incorrect. What should I do to display for example Russian?
I am downloading and displaying a string as following:
System.Net.WebClient wc = new System.Net.WebClient();
webBrsr.DocumentText = wc.DownloadString(url);
The problem is with the WebClient and how it is interpreting the string encoding. One solution is to download the data as raw bytes and parse it out manually:
Bytes[] bytes = wc.DownloadData("http://news.google.com/news?edchanged=1&ned=ru_ru");
//You should really inspect the headers from the response to determine the exact encoding to use,
// this example just assumes UTF-8 which might work in most scenarios
String t = System.Text.Encoding.UTF8.GetString(bytes);
webBrsr.DocumentText = t;
Im making a request to a url, but in the returned string i get foreach " an \"
This is my code:
WebClient webclient = new WebClient();
byte[] databuffer = webclient.DownloadData(url);
return Encoding.UTF8.GetString(databuffer);
What could be the problem to return as content of the webpage for instance:
<div id=\"whatever\"> instead <div id="whatever">?
There's no problem, you are probably looking the result in Visual Studio Debugger which does this. The actual string you are getting doesn't have any \". Try saving it to a file and you will see:
File.WriteAllBytes(#"c:\test.htm", databuffer);
So no worries, unless the web page you are downloading is crap and is using \" instead of " in the response.