Retrieving POST data in C# ASP.NET - c#

I'm having troubles to make my program work correctly - here I explain :
I have, on one hand a C# WinForms app which launches an instance of IE by using the "Navigate" method : myWebBrowser.Navigate(myUrl, "_blank", intarray, "");, with intarray defined like this : byte[] intarray = BitConverter.GetBytes(id);. On this side, it works.
On the other side, I have an ASP .NET WebForms application which has to retrieve this intarray. I've tried this.
if (HttpContext.Current != null)
{
if (Session["Authenticated"] == null)
{
var current = HttpContext.Current;
byte[] postdata = getpostdata(current);
}
}
private byte[] getpostdata(HttpContext CurrentContext)
{
MemoryStream ms = new MemoryStream();
CurrentContext.Request.InputStream.CopyTo(ms);
byte[] postdata = ms.ToArray();
return postdata;
}
// Convert a byte array to an Object
public int ByteArrayToInt(byte[] arrBytes)
{
if (BitConverter.IsLittleEndian) Array.Reverse(arrBytes);
int i = BitConverter.ToInt32(arrBytes, 0);
return i;
}
The problem seems to be in retrieving the Data in the getpostdata(HttpContext) function... I get a byte array with length = 0 instead of the one which is sent with length = 4...
Does anyone know how to make it work ?
Yann

var current = HttpContext.Current;
var sr = new StreamReader(Request.InputStream, Encoding.Default);
var postdata = sr.ReadToEnd();
above

Related

Is it possible to pass multiple arguments value of a function into another function using bytes []?

Need some help to write a function located inside "Application A" to pass multiple variable values using byte [] into another function located in a different application "Application B". So far, I am able to send one variable value but I am really struggling to pass multiple variable values. Thanks in advance for the help, happy holidays!
Application A code:
public static void eventVideoData(string appID, int ID)
{
string application_ID = appID;
int idP = ID;
string pubHeader = "/SERVICES/REQUEST/ECG/UDCMGR/";
//Client.Publish(topic, message, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
byte[] bytes = Encoding.ASCII.GetBytes(application_ID);
progrm.ADCSOAPub.Publish(pubHeader + "VIDEODATA", bytes);
Console.WriteLine("Message Sent");
}
Application B code:
if (e.Topic == "/SERVICES/REQUEST/ECG/UDCMGR/IMAGEDATA")
{
//Logic for message received?
Console.WriteLine(msg1);
string pubHeader = "/SERVICES/RESPONSE/ECG/UDCMGR/";
Client.Publish(pubHeader + "IMAGEDATA", Encoding.ASCII.GetBytes("1"));
Console.WriteLine("Message Sent to Scripting Engine");
}
you could create a tuple and send as byte array then covert back to tuple retrieve the property values, this may not be elegant but you could achieve what you wanted.
private byte[] ApplicationA()
{
(string topic, string message) obj = (
topic: "topic",
message: "message"
);
var bf = new BinaryFormatter();
using MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
private (string,string) ApplicationB(byte[] input)
{
var bf = new BinaryFormatter();
using var ms = new MemoryStream(input);
var obj = bf.Deserialize(ms);
return ((string, string))obj;
}
[Fact]
public void Test()
{
var byteArray = ApplicationA();
var t = ApplicationB(byteArray);
Console.WriteLine(t.Item1);
Console.WriteLine(t.Item2);
}

Convert byte[] to Base64 string with buffer (to avoid OOM)

I need to convert a byte array into a Base64 string. Quite an easy task, really. Just do something like this:
string resultBase64 = Convert.ToBase64String(byteArray);
And it's done, right? Well, not in my case. Apparently, the server this application has to run on has some severe restrictions and an attempt to convert a 20 MB file causes an System.OutOfMemoryException.
I tried to cut the byteArray into chunks and get a Base64 string through a buffer.
int bufferSize = 3000;
System.Text.StringBuilder builder = new System.Text.StringBuilder();
for (int i = 0; i < byteArray.Length; i += bufferSize)
{
int currentBufferSize = Math.Min(bufferSize, byteArray.Length - i);
byte[] buffer = new byte[currentBufferSize];
Array.Copy(byteArray, i, buffer, 0, currentBufferSize);
builder.Append(Convert.ToBase64String(buffer));
}
string resultBase64 = builder.ToString();
However, this throws an OOM as well (I believe, because StringBuilder creates a new string on .ToString, which combined with builder itself causes an OOM).
What is the best way to turn byte[] into base64 string with minimum use of memory?
If you're using Json.NET to turn your object into JSON, it turns out that this already supports turning a byte array into a base64-encoded string, and in a way which doesn't take too much memory.
The trick is to make sure that you can stream the generated JSON directly to the HTTP request, rather than generating the entire request in memory before sending it.
public class TestModel
{
public byte[] Bytes { get; set; }
}
public static void Main(string[] args)
{
var model = new TestModel() { Bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 } };
var request = WebRequest.CreateHttp("...");
request.Method = "POST";
request.SendChunked = true;
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
using (var writer = new JsonTextWriter(streamWriter))
{
new JsonSerializer().Serialize(writer, model);
}
var response = request.GetResponse();
}
The important points here are the request.SendChunked = true, and the fact that we open a new JsonTextWriter over the stream returned from request.GetRequestStream(), and serialize using the JsonTextWriter.

Stream to Base64String - Out of memory exception

Help appreciated in resolving the 'Out of memory exception' from the below source at line return Convert.ToBase64String(stream.ToArray());
private static string GetSPFileBinary(ClientContext ctx, string fileUrlPath)
{
ctx.RequestTimeout = Int32.MaxValue;
var spFile = ctx.Web.GetFileByServerRelativeUrl(fileUrlPath);
var spFileContent = spFile.OpenBinaryStream();
ctx.Load(spFile);
ctx.ExecuteQuery();
MemoryStream stream = new MemoryStream();
if (spFileContent != null && spFileContent.Value != null)
{
spFileContent.Value.CopyTo(stream);
}
return Convert.ToBase64String(stream.ToArray());
}
Assuming that stream.ToArray() was executed successfully and Convert.ToBase64String crashes, you could always convert a batch to 64 string and store it somewhere from where you can reload it. And then load the converted chunks one-by-one and store them into the result file. If your application crashes on stream.ToArray(), then you will need to allow your application to use more memory if available or apply the chunk loading idea when you load from stream as well.
even the below throws the same old 'Out of memory exception', help appreciated.
private static string GetSPFileBinary(ClientContext ctx, string fileUrlPath)
{
ctx.RequestTimeout = Int32.MaxValue;
var spFile = ctx.Web.GetFileByServerRelativeUrl(fileUrlPath);
var spFileContent = spFile.OpenBinaryStream();
ctx.Load(spFile);
ctx.ExecuteQuery();
MemoryStream stream = new MemoryStream();
if (spFileContent != null && spFileContent.Value != null)
{
spFileContent.Value.CopyTo(stream);
}
byte[] docBytes = stream.ToArray();
return Convert.ToBase64String(docBytes);
}

How to read from Response.OutputStream in C#

I'm using ServiceStack to create a Service. In one of the methods I write some data in response's output stream like this: await response.OutputStream.WriteAsync(Consts.DATA, 0, Consts.DATA.Length); where data is a byte[]. Ok, now the problem is when using this service from a client and GET the HttpResponseMessage for the specified method, I don't know how to get the data out. I wan't to unit test that the actual response contains the data I passed when writing to output content.
(I've tried to ReadAsByteArray but it throws an exception. When reading as Stream or String I don't know how to make it a byte[] to compare it in the test with the excpected Consts.DATA byte array.)
I'm pretty new to the field, excuse my ignorance if i miss something. Any help is appreciated.
Thanks!
If you want to use the typed client, you can do (from the documentation):
As raw bytes:
byte[] responseBytes = client.Get<byte[]>("/poco/World");
var dto = responseBytes.FromUtf8Bytes().FromJson<PocoResponse>();
dto.Result //Hello, World
Or as a Stream:
using (Stream responseStream = client.Get<Stream>("/poco/World")) {
var dto = responseStream.ReadFully()
.FromUtf8Bytes()
.FromJson<PocoResponse>();
dto.Result //Hello, World
}
Or even access the populated HttpWebResponse object:
HttpWebResponse webResponse = client.Get<HttpWebResponse>("/poco/World");
webResponse.Headers["X-Response"] //World
using (var stream = webResponse.GetResponseStream())
using (var sr = new StreamReader(stream)) {
var dto = sr.ReadToEnd().FromJson<PocoResponse>();
dto.Result //Hello, World
}
You can also use untyped ServiceStack client to access raw response, it is described in the documentation with samples.
If all you need is load the data from an URL as a Stream and youre using a HttpClient you can just do this to get a stream back:
var result = await client.GetStreamAsync(URL);
I think you can also use GetResponseStream() on your HttpWebResponse.
var result = message.GetResponseStream();
Run your output stream through this static method:
public static byte[] ReadFullStream(Stream st)
{
var lockTaken = false;
try
{
Monitor.Enter(_lock, ref lockTaken);
var size = 0;
var continueRead = true;
var buffer = (byte[])Array.CreateInstance(typeof(byte), 0x10000);
using (MemoryStream ms = new MemoryStream())
{
while (continueRead)
{
size = st.Read(buffer, 0, buffer.Length);
if (size > 0)
{
ms.Write(buffer, 0, size);
}
else
{
continueRead = false;
}
}
return ms.ToArray();
}
}
finally
{
if (lockTaken) { Monitor.Exit(_lock); }
}
}
EDIT: forgot, you'll need this defined at class scope:
private static readonly object _lock = new object();
Read response stream as follow-
using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
{
responseContent = reader.ReadToEnd();
if (response.StatusCode == HttpStatusCode.OK && response.StatusDescription.ToUpper() == "OK" && !string.IsNullOrEmpty(responseContent))
{
objFSLstGetAgent = JsonConvert.DeserializeObject<YourObjectClass>(responseContent);
}
}

Is there anything wrong with this RC4 encryption code in C#

I am trying to listen to the Foxycart XML Datafeed in C# and running into an issue which boils down to encryption.
In short, they send over their data as encoded and encrypted XML using RC4 encryption.
To test, they have some (user submitted) sample code to test this with C#. I tried using this sample RC4 decryption code provided by one of the users but it doesn't seem to work and their support staff thinks its down with the C# RC4 algorithm. Since they are not C# experts, i figured i would ask here. Here is the post on the FoxyCart forum
Anyway, here is the code that (tries to) simulate the response by encrypting an XML file and posting it to a URL (NOTE that DataFeedKey is a string that i have stored as a member variable):
public ActionResult TestDataFeed()
{
string transactionData = (new StreamReader(#"D:\SampleFeed.xml")).ReadToEnd();
string encryptedTransactionData = RC4.Encrypt(DataFeedKey, transactionData, false);
string encodedTransactionData = HttpUtility.UrlEncode(encryptedTransactionData, Encoding.GetEncoding(1252));
string postData = "FoxyData=" + encodedTransactionData;
var req = (HttpWebRequest)WebRequest.Create("http://localhost:3396/FoxyCart/RecieveDataFeed");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
var sw = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
sw.Write(postData);
sw.Close();
HttpWebResponse resp = null;
try
{
resp = (HttpWebResponse)req.GetResponse();
string r = new StreamReader(resp.GetResponseStream()).ReadToEnd();
}
catch (WebException ex)
{
string err = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
}
return null;
}
and here is the callback method that receives the response.
[ValidateInput(false)]
public ActionResult RecieveDataFeed(FormCollection collection)
{
string unencodedFeed = HttpUtility.UrlDecode(collection["FoxyData"], Encoding.GetEncoding(1252));
string transaction = RC4.Decrypt(DataFeedKey, unencodedFeed, false);
return Content("foxy");
}
Instead of posting the whole RC4 class inline in this question, here is a link to the code of this RC4 class.
As i posted in the above link at the top of the question, the issue is when i check the variable transaction inside the
RecieveDataFeed()
method, i should have the regular XML back but instead i see this:
É?xø´ v´“Û·8êUŸí¥MïSÅJÖó5Cå7ã…ÄlÞ&þòG·¶ÝÙ3<ÍÖ¡«úüF¿¿ßìNµ>4¦Äu÷¼Â;£-w¤ƒûÊyL¹®½èíYö½’é(µJŒ~»»=3¼]F‡•=±Ùí]'鳫"øPç{Ù^yyéå–°ñ…5ðWF$zÉnÄ^_”Xë’ï%œ-5á
ÒÛ€jŠt`Â9œÇÞLU&¼~ç2îžúo/¢¶5,º*öOqÝ—‘.ó®šuf™å5G—õC®‰ÁéiÇúW®¦ÝÚ•Z±:„Q\p"p
ôÔiÛ!\D"ÉÂX3]ƒ°è€Œ«DQE‡kÝ#àö`gpöŽ÷nÛ={µÏßKQKüå(ö%¯¯Ü–9}¨¬°£7yo,«”ÜëCÍ/+…†ÕËî‘‹‰AÚmÇÙå©&©¡xÙkŒföX¯ÃX&×°S|kÜ6Ô°Üú\Ätóü-äUƆÈáÅ\ ’E8‚¤âÈ4Ž¾«ãΚ_Sï£y‰xJº•bm*jo›‰ÜW–[ô†ÆJÐà$½…9½šžˆ_ÙÜù/®öÁVhzŠ¥ú(ñ£²6ˆb6¢ëße¤oáIðZuK}ÆÙ]"T¼*åZêñß5K—½òQSåRN Çë'Å¡
ÕyiÈX •bØðIk¿WxwNàäx®‹?cv+X™¥E!gd4â¤nÔ‹¢½Ð”ªÊ­Q!‚.e8s
Gyª4¼ò,}Yœ‚¹”±E‡Jy}Sæ
ƒ¦ýK'Ð}~B¦E3!0°ú´A–5Þ³£9$–8äÏ©?
œ‡8GÂø
The code looks right:
Encrypt
Encode
Decode
Decrypt
but it doesn't seem to be working. Any suggestions on what might be wrong above?
I am a bit surprised by the code in the CR4 class. I can't see how it would work reliably.
The code uses windows-1252 encoding to encode characters into bytes, then it encrypts the bytes and tries to decode the bytes into characters. That won't work reliably, as you can only decode bytes that comes from encoding characters.
The method takes a string and returns a string, but it should take a byte array and return a byte array, similar to how all the encryption classes in the framework does it.
Here is a version that works like that:
public class RC4 {
public static byte[] Encrypt(byte[] pwd, byte[] data) {
int a, i, j, k, tmp;
int[] key, box;
byte[] cipher;
key = new int[256];
box = new int[256];
cipher = new byte[data.Length];
for (i = 0; i < 256; i++) {
key[i] = pwd[i % pwd.Length];
box[i] = i;
}
for (j = i = 0; i < 256; i++) {
j = (j + box[i] + key[i]) % 256;
tmp = box[i];
box[i] = box[j];
box[j] = tmp;
}
for (a = j = i = 0; i < data.Length; i++) {
a++;
a %= 256;
j += box[a];
j %= 256;
tmp = box[a];
box[a] = box[j];
box[j] = tmp;
k = box[((box[a] + box[j]) % 256)];
cipher[i] = (byte)(data[i] ^ k);
}
return cipher;
}
public static byte[] Decrypt(byte[] pwd, byte[] data) {
return Encrypt(pwd, data);
}
}
Example:
string data = "This is a test.";
byte[] key = { 1, 2, 3, 4, 5 };
// encrypt
byte[] enc = RC4.Encrypt(key, Encoding.UTF8.GetBytes(data));
// turn into base64 for convenient transport as form data
string base64 = Convert.ToBase64String(enc);
Console.WriteLine(base64);
// turn back into byte array
byte[] code = Convert.FromBase64String(base64);
// decrypt
string dec = Encoding.UTF8.GetString(RC4.Decrypt(key, code));
Console.WriteLine(dec);
Output:
5lEKdtBUswet4yYveWU2
This is a test.
Although this is more shooting in the dark... I am rather certain that the class implementing RC4 looks like it is assuming everyting is either ASCII or CodePage 1252 - both is wrong because I assume that the XML supplied is UTF-8 and .NET string represantion in memory is UTF16...
If my assumption is right the data is already scrambled when you get it back from encryption...
EDIT - some links to working RC4 code in C#:
http://tofuculture.com/Blog/post/RC4-Encryption-in-C.aspx
http://dotnet-snippets.com/dns/rc4-encryption-SID577.aspx
http://www.codeproject.com/KB/recipes/rc4csharp.aspx
http://icodesnip.com/snippet/csharp/rc4-encryption-code-snippets

Categories

Resources