I'm new to Windows phone 8 Dev and C#. I try to get String from remote server. This is my code:
var client = new HttpClient();
var response = await client.GetAsync(MyUrl);
string charset = GetCharset(response.Content.Headers.ContentType.ToString());
Debug.WriteLine("charset: " + charset); \\debug show: charset: ISO-8859-1
var bytesCharset = await response.Content.ReadAsByteArrayAsync();
Debug.WriteLine(Encoding.UTF8.GetString(bytesCharset,0,bytesCharset.Length));
//Debug show: `m\u1EF9 t�m`
m\u1EF9 t�m is what I got, but I expect it's Vietnamese: Mỹ Tâm. Can anyone give me an idea to get expected result? (I think the bytes is encoded in IOS-8859-1).
Encoding.UTF8 should be interpreting your data correctly, as ISO-8859-1 is a subset of UTF-8. However, so that other encodings are handled transparently, it is probably better to use HttpContent's ReadAsStringAsync() method instead of ReadAsByteArrayAsync().
I suspect you have the correct string there but your debug window simply cannot display those two unicode characters properly.
You can use the static GetEncoding method to request a specificy codepage.
e.g.:
var encoding = Encoding.GetEncoding("ISO-8859-1");
Finally I found the solution. After using Json.Net to parse json from remote server, my problem's gone, and I can get expected result:
JArray jArray = JArray.Parse(isoText);
String exptectedText= jArray[1].ToString();
Actually, I dont know why my code works :( Thank you anyway!
Related
I am using the following code to encode the SAMLRequest value to the endpoint, i.e. the XYZ when calling https://login.microsoftonline.com/common/saml2?SAMLRequest=XYZ.
Is this the correct way to encode it?
private static string DeflateEncode(string val)
{
var memoryStream = new MemoryStream();
using (var writer = new StreamWriter(new DeflateStream(memoryStream, CompressionMode.Compress, true), new UTF8Encoding(false)))
{
writer.Write(val);
writer.Close();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length, Base64FormattingOptions.None);
}
}
If you just want to convert string to a base64 encoded string, then you can use the following way:
var encoded = Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(val));
Console.WriteLine(encoded);
return encoded;
Yes, that looks correct for the Http Redirect binding.
But don't do this yourself unless you really know what you are doing. Sending the AuthnRequest is the simple part. Correctly validating the received response, including guarding for Xml signature wrapping attacks is hard. Use an existing library, there are both commercial and open source libraries available.
I am getting xml data through an httpwebrequest. The following code was working fine. But something changed and suddenly started to give me an exception on Read() method with error : The '', Hexadecimal value 0x1F, is not a valid character. Line 1, Item 1. In web browser sURL gives me a valid xml. I dont know what changed.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sURL);
req.Method = "GET";
WebResponse response = req.GetResponse();
StreamReader resStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
XmlTextReader reader = new XmlTextReader(resStreamReader);
while (reader.Read())
{
// some code
}
At This MSDN Topic Same problem discussed, and result is that,
Servers sometimes compress their responses to save on bandwidth, when this happens, you need to decompress the response before attempting to read it. Fortunately, the .NET framework can do this automatically, however, we have to turn the setting on.
And this stackoverflow topic solves the problem
0x1F XML Error Solution
You should check the response from your url about possible GZip compression usages.
Have a nice day.
We have a legacy NodeJS API we are looking to move away from. We are extracting the auth component out of it.
We have a function that hashes a password, that looks a bit like:
var sha256 = crypto.createHash('sha256');
sha256.update(password + secret);
return sha256.digest('hex');
Aside from the obvious security implications a function like this has, it's encoded using the binary encoding NodeJS has.
If you pass in a String to update, it will use 'binary' as the encoding format. This results in it actually encoding unicode characters like "Kiełbasa" as "KieBbasa", before SHA256'ing them.
In our Scala code we are now looking to rewrite this legacy function so we can auth old users. But we cannot find a charset to use on these strings that has the same resulting output. Our Scala code looks like:
def encryptPassword(password: String): String = {
Hashing.sha256().hashString(in, Charsets.UTF_8).toString
}
We need the in string to be the same as what Node is using, but we can't figure it out.
Ideas? Node.js... not even once.
Well, turns out this is much easier than it seems. The following code works:
def encryptPassword(password: String): String = {
val in = (password + secret).map(_.toByte.toChar).mkString("")
Hashing.sha256().hashString(in, Charsets.UTF_8).toString
}
Thanks #stefanobaghino
I am trying to receive a base64 string (which is an image) in my web api web service.
The issue I am having is with the length of the string, is there a way I can set the limit of of incoming strings to allow for a string of a massive size? The string has already been encoded for url use. From what I can tell this is my issue. Right now if I call the function it gives me an 404 error. However if I remove the base64string and put in just a normal string it works.
This is my function
public int userLogin(string identifier, string name, string hometown, string photo)
{
da = new SqlDataAdapter();
ds = new DataSet();
command = new SqlCommand("p_userLogin", connection);
command.Parameters.AddWithValue("#userInfo", identifier);
command.Parameters.AddWithValue("#name", name);
command.Parameters.AddWithValue("#hometown", hometown);
command.Parameters.AddWithValue("#photo", photo);
command.CommandType = CommandType.StoredProcedure;
da.SelectCommand = command;
da.Fill(ds);
int userID = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
return userID;
}
And this is the base 64 string I am trying to send
%2F9j%2F4AAQSkZJRgABAgAAAQABAAD%2F%2FgAEKgD%2F4gIcSUNDX1BST0ZJTEUAAQEAAAIMbGNtcwIQAABtbnRyUkdCIFhZWiAH3AABABkAAwApADlhY3NwQVBQTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA9tYAAQAAAADTLWxjbXMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAApkZXNjAAAA%2FAAAAF5jcHJ0AAABXAAAAAt3dHB0AAABaAAAABRia3B0AAABfAAAABRyWFlaAAABkAAAABRnWFlaAAABpAAAABRiWFlaAAABuAAAABRyVFJDAAABzAAAAEBnVFJDAAABzAAAAEBiVFJDAAABzAAAAEBkZXNjAAAAAAAAAANjMgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB0ZXh0AAAAAEZCAABYWVogAAAAAAAA9tYAAQAAAADTLVhZWiAAAAAAAAADFgAAAzMAAAKkWFlaIAAAAAAAAG%2BiAAA49QAAA5BYWVogAAAAAAAAYpkAALeFAAAY2lhZWiAAAAAAAAAkoAAAD4QAALbPY3VydgAAAAAAAAAaAAAAywHJA2MFkghrC%2FYQPxVRGzQh8SmQMhg7kkYFUXdd7WtwegWJsZp8rGm%2FfdPD6TD%2F%2F%2F%2FbAEMABQMEBAQDB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hKLsOqYregkJ%2FsCnMbIlBeeQ6bd4HMt7gSlt3g294Ni5wf%2FxAAnEAEAAgIABgICAgMAAAAAAAABABEhMUFRYXGBkRDwIKGxwdHh8f%2FaAAgBAAABPxD8bHBvyAtgSxubWaeyjq3OWmhRcWa5BGuE702QdPYj5fh%2BWLBlL3GAokoQq7dXgYYJuyUWKCqNrMBct19ib7rYxnGmY9RPIjrFx7qNJlNYUH8x%2FB3OmW8%2FjJkUoBss3jSnmAoWwqwwvHHq6hStcYl5NWiZyjeWGrZBwArVeX3DtqbgEUuQIPEflgkW35Ciyo7Zf1cIFq8yPv8AzGvE029aczugohlJgKoQa78%2FEfik06SiWQbYMy7BGLHsHL4gMPQBHKEsC1PEqUcDUxvtcBUBzdDnAkkL4UwK4bOJu%2Be%2Fhm0Uxt5VHo3t5%2B2IQxVlnJLe4J1imuUmbyK8C%2BssiZbu8VuFIDhX79QPiMEE2tf8QNTVZsL61sHKznGcZ7JWSigchks6B6VCjzgL6NBh8RtB77jjuODlgMIAlDSf1cGoiTwUbXGsNrdpqgAKcRoBCmkrYDTV3DM1O5T2Fd25g4SuNI9OxqACkmq5A4jCbarwqbiXHdl%2Fo8RcALYRTkcwgTUNq9x3BYURVZikFM1dPgiGZ1v%2FAHxRft94XAh3ojx6zGRwwGliiTcA5LTESZAtbwDiXeAq%2FwC8QFpj75gF%2Bn5hS%2Bn5n%2F%2FZ
Any input on this would be appreciated.
I don't know whether you got the answer or not, because question had posted long time back.
I also faced same problem,resolved my issue using
below link may help you to find the answer #Ken
ASP.NET Web API does not allow Lengthy base64 URI
I use javamail to get message, when I get the message i have:
com.sun.mail.util.BASE64DecoderStream,
I know that is part of multipart message, in the source of message I have
Content-Type: image/png; name=index_01.png
Content-Transfer-Encoding: base64
How encode this message??
edit:
I have that code:
else if (mbp.getContent() instanceof BASE64DecoderStream){
InputStream is = null;
ByteArrayOutputStream os = null;
is = mbp.getInputStream();
os = new ByteArrayOutputStream(512);
int c = 0;
while ((c = is.read()) != -1) {
os.write(c);
}
System.out.println(os.toString());
}
And that code return strange string, for example:
Ř˙á?Exif??II*????????????˙ě?Ducky???????˙á)
com.sun.mail.util.BASE64DecoderStream is platform dependent. You cannot rely on that always being the type of class that handles base64 decoding.
Rather the javamail APIs already support decoding for you:
// part is instanceof javax.mail.Part
ByteArrayOutputStream bos = new ByteArrayOutputStream();
part.getDataHandler().writeTo(bos);
String decodedContent = bos.toString()
What are you expecting when you read the contents of an image part? The image is stored in the message in an encoded format, but JavaMail is decoding the data before returning the bytes to you. If you store the bytes in a file, you can display the image with many image viewing/editing applications. If you want to display them with your Java program, you'll need to convert the bytes to an appropriate Java Image object using (e.g.) APIs in the java.awt.image package.
That Sun's base 64 encoder is in the optional package and can be moved or renamed at any time without warning, also may be missing in alternative Java runtimes, also access to these packages may be disabled. It is much better not to rely on it.
I would say, use Base64 from Apache Commons instead, should do the same. Hope you can rebuild and fix the source code.