Encoding my string to send an http request via C# - c#

I am trying to send a simple HTTP request like this:
var client = new WebClient();
string myString="this is the string i want to send";
message = client.DownloadString("http://www.viralheat.com/api/sentiment/review.xml?text=" + myString + "&api_key="+currentKey);
but some of the strings I send includes # or & or such characters, so I want to encode the string first before sending it, because it throws an error if it includes these special characters without being encoded.

Call Uri.EscapeDataString.
Unlike HttpUtility, this works on the client profile too.

Use HttpUtility.UrlEncode

Related

How to use call http from c#

I am trying to send an sms from my website.
Below is the HTTP api which works perfectly.It sends the msg and returns the string
http://sms.mywebsite.com/api/sendmsg.php?user=MYID&pass=MYPASS&sender=SENDERID&phone=1234567980&text=Test Message&priority=ndnd&stype=normal
But i want to use it in C#.Accept mobile number from TextBox1 and Message from TextBox2
WebRequest webRequest = WebRequest.Create("http://sms.mywebsite.com/api/sendmsg.php?user=MYID&pass=MYPASS&sender=SENDERID&phone=" + TextBox1.Text + "&text=" + TextBox2.Text+ "&priority=ndnd&stype=normal")
The first statement is executing if i paste the http code directly in the website and i recieve the smsin my mobile .But the WebRequest statement dosent send the sms
TextBox1.Text=123456789;//some mobile number
TextBox2.Text="Thankyou for registering # MyWEBSITE. A verification email has been sent to Your email";
You seem to be only creating a web request object and not executing it.
var response = webRequest.GetResponse();
Refer to the documentation # https://msdn.microsoft.com/en-us/library/bw00b1dc(v=vs.110).aspx
I would also recommend you use a HttpClient instead.
You should use Server.UrlEncode. Probably, you have some spaces and special characters in the text message.
WebRequest webRequest = WebRequest.Create(Server.UrlEncode("http://sms.mywebsite.com/api/sendmsg.php?user=MYID&pass=MYPASS&sender=SENDERID&phone=" + TextBox1.Text + "&text=" + TextBox2.Text+ "&priority=ndnd&stype=normal"))

Sending query parameters using C#

I want to send a string to a web API using GET. My string contains a few characters that are not accepted in URL or need to be escaped. How can I scape them and send them to the server?
If it helps, my server is written in PHP so any encoding needs to be reversed in PHP.
If your data has HTTP Wildcards in it try to serialize or encode it that outputs a string doesn't contain any HTTP Wildcards.You can pass data with uriencode or some other encoding algorithm like for example (base64) :
string byteArray = System.Encoding.UTF8.GetBytes(yourData);
string encoded = Convert.ToBase64String(byteArray);
string exampleUri="a.com/b.php?data="+encoded;
Decode it in PHP like this :
$data = base64_decode($_GET["data"]);
As i said you can also use uriencoding with :
HttpUtility.UrlEncode / UrlDecode (You need System.Web assembly in your project)

C# HttpWebRequest GET partially encoded url

When I send a get using HttpWebRequest is seems to turn it into a uri, run it through an encoder and send the encoded string. When I look at my address in the request after it is created I have the OriginalString which is correct and an AbsoluteUri which is encoded and incorrect. My code and example urls are below.
HttpWebRequest webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
String responseData = WebResponseGet(webRequest);
OriginalString:"https://api.linkedin.com/v1/people/url=https%3A%2F%2Fwww.linkedin.com%2Fin%2Ffirstmlast"
AbsoluteUri:"https://api.linkedin.com/v1/people/url=https%3A//www.linkedin.com/in/firstmlast"
How can I force HttpWebRequest to send my original string that I passed it and not a uri? Also I cannot send the already encoded string as a query string, LinkedIn requires it to be apart of the url.
I found a HackedUri class here: http://blogs.msdn.com/b/xiangfan/archive/2012/01/16/10256915.aspx and created my request like this passing it a "Hacked Uri" instead of a string. This seems to be a security limitation problem with .Net.
HttpWebRequest webRequest = System.Net.WebRequest.Create(HackedUri.Create(url)) as HttpWebRequest;
Have you tried double-encoding the relevant part of the URL?
var request = WebRequest.CreateHttp("https://api.linkedin.com/v1/people/url=" + HttpUtility.UrlEncode("https%3A%2F%2Fwww.linkedin.com%2Fin%2Ffirstmlast"));

send "#" and "&" character through XML request

I need to send special character through XML request. to complete this action, I convert the string to UTF-8, as XML always use UTF-8 data encoding.[using below code]
byte[] myBytes = Encoding.Default.GetBytes(MyMessage);
MyMessage = Encoding.UTF8.GetString(myBytes);
I am able to send all the special characters like "!#$%^*()<>?" except "#" and "&". But those two character in very vital for me to send. how can I send those two character through XML.
I also try to replace and send using
MyMessage= MyMessage.Replace("&", "&");
MyMessage= MyMessage.Replace("#", "#");
but it also doesn't work.
I have to create a request and send to a URL: sample requesting format is below:
http://1.2.3.4/Default.aspx?MSG_ID=20140107032647101768&BODY=test#&Time=20140107032647102769
Replace & by %26 and # by %23.
This is not XML specific, it's a URL issue. To catch all issuey, try HttpUtility.UrlEncode

C#: Sending HTTP GET request without UTF-8 encoding

I need to send HTTP GET request from C# to CLASSIC ASP service.
The service is built in a way that it decodes the data from the QueryString using Windows-1255 encoding, rather than the standard UTF-8.
It seems that HttpWebRequest class always encodes GET data with UTF-8 and it doesn't work for me. Is there any way to send HTTP GET request from C#, while GET data is encoded with different than UTF-8 encoding?
Thanks.
You need to set a header on your get request:
Content-Type:text/xml; Charset=windows-1255
HttpRequest r = new HttpRequest(.....);
r.Headers.Add("Content-Type", "text/xml; Charset=windows-1255");
Maybe this post will be of some use too:
Read non-english characters from http get request
Ok, I finally got the answer.
First of all, specifying ContentType in the header doesn't work.
If destination URL is containing none-English letters, the HttpWebRequest will always use UTF-8 + URLEncode to build the final URI the request is sent to.
To use encoding different from UTF-8 I needed to encode URL values by myself (instead of providing necessary encoding to HttpWebRequest as I expected).
Following function that builds HTTP GET URL, while values are encoded with any requested encoding (and not with the default UTF-8):
string BuildData(NameValueCollection getData, Encoding enc)
{
StringBuilder urldata = new StringBuilder();
for (int i = 0; i < getData.Count; i++)
{
if (i > 0) urldata.Append("&");
urldata.Append(getData.Keys[i] + "=" + HttpUtility.UrlEncode(enc.GetBytes(getData[i])));
}
return urldata.ToString();
}
The HttpWebRequest can be used with something like
"http://get-destination.com/submit?" + BuildData(keysAndValues, Encoding.GetEncoding(1255));
In this case HttpWebRequest gets already encoded URL which doesn't contain none-English letters and it keeps it as is.

Categories

Resources