String Variable Character Limit - c#

My String variable only stores 4096 characters, I need to store more, how can i achieve that?
Below is what i am trying to do
ServiceController[] myServices = ServiceController.GetServices();
String ServiceList = "";
foreach (ServiceController service in myServices)
{
ServiceList += service.DisplayName + "|||";
}
return ServiceList;
When the variable is returned, it only stores 4096 characters and rest are trimmed off.
P.S. I need them in one variable as I am making a URL out of them and passing to my webservice.

I need them in one variable as I am making a URL out of them and passing to my webservice.
No, don't do that!
A 4096 character URL is a very bad idea and is not guaranteed to work.
Extremely long URLs are usually a mistake. URLs over 2,000 characters will not work in the most popular web browser. Don't use them if you intend your site to work for the majority of Internet users.
(source)
Make a shortened URL that contains an id. Store the rest of the information in a database with the short id as the key.
Related
What is the maximum length of a URL in different browsers?
Maximum URL length is 2,083 characters in Internet Explorer

.NET string length limit is 2 billion characters.
Browsers do have a limit on how long of a URL they will accept, and the length limit is different across browser implementations. IE's limit is typically the shortest, at around 2k last time I checked in the IE6 era. Firefox and Chrome are considerably higher than that, but there is still a limit.

Your problem is elsewhere - but it is not possible to reliably use a URL that uses more than 2000 characters, you will need another approach entirely - see this SO answer: What is the maximum length of a URL?
(Also for building large strings use a StringBuilder instead)

Strings in C# has about 2Gig limit.so there is no problem with your string variable

Related

Combined length of user strings used by the program exceeds allowed limit. Try to decrease use of string literals

I am using c#.
I need to store 10mb data(base64 coded) in string/string builder and send it to sql server db.
I tried different ways but getting 'Combined length of user strings used by the program exceeds allowed limit. Try to decrease use of string literals." error.
tried below.. getting error
string str1="very large text...";
string str2="very large text..";
string tr3 = str1+str2;
please advise me how to achieve this?
Basically i want to send that string(base64) to api, and i don't want to store that file somewhere and read it.

Is it OK to return a string with the size around 2 MB from a C# method?

I have a situation where I need to return the MAX of 2MB sized string from a method.
I have seen some web services returning big XML contents as strings. What is the optimum size with which we should transact in value parameter passing and returning?
There is no technically correct answer to this because it's perfectly normal to pass any amount of characters as long as it's needed in your program. You need to evaluate if passing such a large string is valueable to your program. If it is, great. If it's not, find a way to shorten it or pass a file path or database identifier instead so the service can get the text from there itself.
Is it OK to return a string with the size around 2 MB from a C#
if it is local (intranet,network) then it is mostly ok (unless the network is v v old). But, if you are passing it over the internet then you should consider the bandwidth of the client on which the data will be sent/received. Will it slow down your page or not etc. In most cases though it will be fine.
What is the optimum size with which we should transact in value parameter passing and returning
Any size that will not cause your application to slow down is fine.
It is completely okay, the only thing you should keep in mind is:
If a user is using 56kbps connection 2024/56=36.14 sec if this is not important for your case then no need to worry.

Invalid length for a Base-64 char array Exception under Chrome

I have to send images generated by a javascript function to the server.
In order to do it, I store the base64 string of the image in a hidden textbox.
myTextBox.value = 'imagedata';
It works well for small size files (1MB or less).
However, when I try to send large files, the server returns an "Invalid length for a Base-64 char array" error.
The weird part is that I get this error with Chrome but not with Internet Explorer 10.
When I check the value of the string with the debugger, it seems like it is truncated with Chrome.
What causes this problem ? Is there a workaround ?
Thank you.
You shouldn't be using a single textbox to hold that much content.
Textboxes can only hold a limited amount of content. Looks like you're hitting that limit in chrome.
If you really must store that much content, then you'll have to break the content across multiple textboxes.
The max size depends from browser to browser.
See here for more information
It could be because QueryString is returning space instead of + (it does that in Chrome, but not I.E.).
Solution:
Decrypt(Request.QueryString["myvar"].Replace(' ', '+'))

Compress a short but repeating string

I'm working on a web app that needs to take a list of files on a query string (specifically a GET and not a POST), something like:
http://site.com/app?things=/stuff/things/item123,/stuff/things/item456,/stuff/things/item789
I want to shorten that string:
http://site.com/app?things=somekindofencoding
The string isn't terribly long, varies from 20-150 chars. Something that short isn't really suitable for GZip, but it does have an awful lot of repetition so compression should be possible.
I don't want a DB or Dictionary of strings - the URL will be built by a different application to the one that consumes it. I want a reversible compression that shortens this URL. It doesn't need to be secure.
Is there an existing way to do this? I'm working in C#/.Net but would be happy to adapt an algorithm from some other language/stack.
If you can express the data in BNF you could contruct a parser for the data. in stead of sending the data you could send the AST where each node would be identified as one character (or several if you have a lot of different nodes). In your example
we could have
files : file files
|
file : path id
path : itemsthing
| filesitem
| stuffthingsitem
you could the represent a list of files as path[id1,id2,...,idn] using 0,1,2 for the paths and the input being:
/stuff/things/item123,/stuff/things/item456,/stuff/things/item789
/files/item1,/files/item46,/files/item7
you'd then end up with ?things=2[123,456,789]1[1,46,7]
where /stuff/things/item is represented with 2 and /files/item/ is represented with 1 each number within [...] is an id. so 2[123] would expand to /stuff/things/item123
EDIT The approach does not have to be static. If you have to discover the repeated items dynamically you can use the same approach and pass the map between identifier and token. in that case the above example would be
?things=2[123,456,789]1[1,46,7]&tokens=2=/stuff/things/,1=/files/item
which if the grammar is this simple ofcourse would do better with
?things=/stuff/things/[123,456,789]/files/item[1,46,7]
compressing the repeated part to less than the unique value with such a short string is possible but will most likely have to be based on constraining the possible values or risk actually increasing the size when "compressing"
You can try zlib using raw deflate (no zlib or gzip headers and trailers). It will generally provide some compression even on short strings that are composed of printable characters and does look for and take advantage of repeated strings. I haven't tried it, but could also see if smaz works for your data.
I would recommend obtaining a large set of real-life example URLs to use for benchmark testing of possible compression approaches.

Creating unique URLs in ASP.NET

In my website, I need to create unique URLs that an admin user would use to send it to a group of users. The unique URL is created whenever an admin creates a new form. I understand I can use a guid to represent unique URLs, but I am looking for something shorter (hopefully around 4 characters, since it's easier to remember). How would I generate a unique URL in ASP.NET that would look like this:
http://mydomain.com/ABCD
I understand some of the URL shortener websites (like bit.ly) does something like this with a very short unique URL. Is there an algorithm I can use?
How about something like
public static string GetRandomString (int length)
{
string charPool = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
StringBuilder sb = new StringBuilder();
Random rnd = new Random();
while ((length--) > 0)
sb.Append(charPool[(int)(rnd.NextDouble() * charPool.Length)]);
return sb.ToString();
}
and call
GetRandomString(4);
Just write an algorithm to select a certain number of characters from a GUID (e.g. the first 4 or 8 characters, every even character up to 4 or 8 characters.)
Be sure to check it against the database to make sure it isn't already in use, and if it is regenerate it. As a safeguard, maybe make a timeout (if it tries to generate 10 and they're all in use, give up,) but it's unlikely to use every possible combination.
I believe bit.ly performs a hash and then base64 encodes the result. You could do the same, although it'll be more than 4 characters. Be sure to add code that handles hashing collisions. You could append 1, 2, 3, etc. when the first hash is in use.
Another approach is to create a new table in a database. Every time you need a new URL, add a row to this table. You could use the PK as the URL value. This will give you up to 10,000 unique values using only four characters. Base64 encode for even more.

Categories

Resources