I have a controller that is building using a StringBuilder called param to create a string to post to a REST service. The string that is passed is a query string that will get parsed. I'm encoding the values for each pair so that ampersands, doesn't inadvertantly create a new key.
An encoded param string might look like
clientType=MyClient&form_id=webform_client_form_38&referrer=http://mywebsite&company=My+%26+Company
Here is the code I'm using to send to the REST service
byte[] formData = UTF8Encoding.Default.GetBytes(param.ToString());
req.ContentLength = formData.Length;
//Send the request:
using (Stream post = req.GetRequestStream())
{
post.Write(formData, 0, formData.Length);
}
When I debug the REST service the string is always encoded back, adding the ampersand back between My and Company.
Is there another built in method that will convert the string to a byte without encoding the text?
You can't use a HTML Decode function? that will transform back the string to the correct string that you sent.
HttpUtility.HtmlDecode
Related
So basically I want to get json string from specific url, and this json string is encrypted become a .txt file. All I want to do is get the encrypted string and decrypt it inside my application.
Here is my HttpWebRequest code to get the response string:
public string GetResponse(url)
{
string responseString = "";
HttpWebRequest webRequest = HttpWebRequest.Create(url) as HttpWebRequest;
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
responseString = reader.ReadToEnd();
}
return responseString;
}
But what I get from the response is actually an unreadable string (only "O")
I already try to convert it to byte array before convert it to Base64 string, but still, the response string is not right.
Thanks for the help.
Unfortunately, i didnt realize that the response string actually includes added character "/0" which i have to remove it first, then i able to decrypt the string.
Thank you very much.
Is there any way to send data (string , files ,,,) In the form of bytes from desktop application to website
You can use the WebClient class to send data in an HTTP request. A string; example:
string url = "http://website.com/MyController/MyAction";
string data = "Something";
string response;
using WebClient client = new WebClient()) {
client.Encoding = Encoding.UTF8;
response = client.UploadString(url, data);
}
Or a byte array; example:
string url = "http://website.com/MyController/MyAction";
byte[] data = { 1, 2, 3 };
byte[] response;
using WebClient client = new WebClient()) {
response = client.UploadData(url, data);
}
In the web application (assuming that you are using MVC and C#) you would have an action method in a controller that gets the data. Example:
public ActionResult MyAction() {
byte[] data = Request.BinaryRead(Request.ContentLength);
// do something with the data
// create a byte array "response" with something to send back
return Content(response, "text/plain");
}
Both a string and a byte array ends up as a byte array when sent, so you would use Encoding.UTF8.GetString(data) to turn the data into the string sent by UploadString.
To return a string for UploadString you would use Encoding.GetBytes(str) to turn a string into bytes.
There are several overloads of those methods that do similar things, that might fit your needs better, but this should get you started.
I am trying to send a POST request in C# with a parameter encoded to ISO-8859. I am using this code:
using (var wb = new WebClient())
{
var encoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
var encodedText = System.Web.HttpUtility.UrlEncode("åæ ÆÆ øØ ø", encoding);
wb.Encoding = encoding;
wb.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
var data = new NameValueCollection();
data["TXT"] = encodedText;
var response = wb.UploadValues(_url, "POST", data);
}
I have figured out that the correctly encoded string for "åæ ÆÆ øØ ø" is %E5%E6+%C6%C6++%F8%D8+%F8, and I can see when debugging that encodedText actually is this string. However when inspecting the raw request in fiddler, I can see that the string looks like this: TXT=%25e5%25e6%2B%25c6%25c6%2B%25f8%25d8%2B%25f8. I am guessing some kind of extra encoding is being done to the string after or during the call to UploadValues().
Thank you so much in advance.
I checked Google for this. According to another question here on SO at UTF32 for WebClient.UploadValues? (second answer), Webclient.UploadValues() indeed does encoding itself. However, it does ASCII encoding. Youll have to use another method to upload this, like HttpWebRequest.
I have an application build on c#. here is the snippet of my code
private void ProcessRequest(IAsyncResult result)
{
HttpListenerContext context = httpListener.EndGetContext(result);
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
NameValueCollection queryString = getQueryStringFromRequest(request);
…….
}
The response I get is in English only. However, sometimes I get part of the response in other language, Like Icelandic. Then my application is not able to format the string accordingly. For example in queryString I get something like this:
{ ENTITY_NAME=Contact&OBJECT_NAME=Mr.+Hl%ufffd%ufffdar+Hl%ufffd%ufffdberg}
However object name should be Mr. Hlíðar Hlíðberg.
I understand that issue is with not able to parse it according to dual byte character. I tried something similar to:
if (queryString["OBJECT_NAME"] != null){
string s_unicode = queryString["OBJECT_NAME"];
System.Text.Encoding iso_8859_1 = System.Text.Encoding.GetEncoding("iso-8859-1");
System.Text.Encoding utf_8 = System.Text.Encoding.UTF8;
byte[] utfBytes = utf_8.GetBytes(s_unicode);
byte[] isoBytes = Encoding.Convert(utf_8, iso_8859_1, utfBytes);
string msg = iso_8859_1.GetString(isoBytes);
}
but still i am getting response like Mr. Hl??ar Hl??berg
but it didn’t worked for me. Also i never liked this method as its to specific and things needs to be hard coded for eg. the key in this case.
So, How do I do it. Can I do something through config file or generic?
I know there are a lot of questions about sending HTTP POST requests with C#, but I'm looking for a method that uses WebClient rather than HttpWebRequest. Is this possible? It'd be nice because the WebClient class is so easy to use.
I know I can set the Headers property to have certain headers set, but I don't know if it's possible to actually do a POST from WebClient.
You can use WebClient.UploadData() which uses HTTP POST, i.e.:
using (WebClient wc = new WebClient())
{
byte[] result = wc.UploadData("http://stackoverflow.com", new byte[] { });
}
The payload data that you specify will be transmitted as the POST body of your request.
Alternatively there is WebClient.UploadValues() to upload a name-value collection also via HTTP POST.
You could use Upload method with HTTP 1.0 POST
string postData = Console.ReadLine();
using (System.Net.WebClient wc = new System.Net.WebClient())
{
wc.Headers.Add("Content-Type","application/x-www-form-urlencoded");
// Upload the input string using the HTTP 1.0 POST method.
byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(postData);
byte[] byteResult= wc.UploadData("http://targetwebiste","POST",byteArray);
// Decode and display the result.
Console.WriteLine("\nResult received was {0}",
Encoding.ASCII.GetString(byteResult));
}