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"))
Related
I've been tasked with building a service which pulls information from a 3rd party API into our internal datawarehouse. They have a get request to pull the data I want where you specify the parameters you want via query strings. E.g.
http://www.api.com?parameter=firstname¶meter=surname
In my code the length of the URL is over 3600 characters long as the requirement is for 116 parameters.
My web request is generated using this code:
private HttpWebRequest GetWebRequest(string url, string type, int timeout)
{
var httpWebRequest = (HttpWebRequest) WebRequest.Create(_baseUrl + url);
httpWebRequest.Method = type;
httpWebRequest.Timeout = timeout;
httpWebRequest.ContentType = "application/json";
httpWebRequest.Headers.Add("Authorization", "Bearer " + _token.access_token);
httpWebRequest.ContentLength = 0;
return httpWebRequest;
}
When I run the code I am getting back a web exception with the message "Unable to connect to the remote server" with an internal exception message of "No connection could be made because the target machine actively refused it IP Address"
I have not included the entire URL in this post as I have found that if I copy and paste the url into Postman and run the request I get the response I expect so I know that the URL is formatted correctly. I have also discovered that if I cut down the length of the url to around 2100 characters the request then works.
I have been searching but have not found any definitive documentation to suggest that there is a limit to the length of the URL, but I can not explain why the whole url works in Postman but not in a c# web request and that if I cut the length of the URL it then works in the web request!
If anyone has any ideas about this I'd be greatfull.
An old post suggests that depending on the server and client the maximum request length is somewhere between 2 - 4 and 8 KB, which is consistent with your observations.
Postman is 'another' client, so it is well possible that it works there while it doesn't in your program. Bottom-line is: you should not GET such long requests. You should POST them instead. If you have control over the service, you could change it so it supports POST too, if not already (documented or not).
I am trying to send the email using exchange web service c#. It works fine if body contains normal text. But the body contains the URL like below is not working. it is sending the mail, but URL is not coming in the receipt end.
url contains < >
http://localhost:52122/Home?Function=ABC&Parameter1=12345
Please help.
Thanks in advance
replace the body content with this code
string Url = "localhost:52122/Home?Function=ABC&Parameter1=12345";
string EmailBody = #"Email Body </br> please " + String.Format("<a href='{0}'>Click here</a>", Url);
I am trying to post a WPF form to my server, but I keep getting a 401 unauthorized response, even though I am sending the correct details.
This is my c# code
// Send the ComputerSettings to the API
String _Url = this.ApiUrl + "/api/hospitals/settings.xml";
String _Parameters = "";
_Parameters += "access_token=" + Authentication.AccessToken;
_Parameters += "&hospital_id=" + txtHospital.Text;
_Parameters += "&username=" + HttpUtility.UrlEncode(txtUsername.Text);
_Parameters += "&password=" + HttpUtility.UrlEncode(txtPassword.Password);
Debug.WriteLine(_Url + "?" + _Parameters);
WebClient _WebClient = new WebClient();
_WebClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
_WebClient.Encoding = System.Text.Encoding.UTF8;
_WebClient.UploadString(_Url, _Parameters);
When I run this from Visual Studio the Server returns a 401 unauthorized. But when I copy the url from Debug into into a REST Chrome Client (I use DHC in case that is important) then POST from there, the request works as expected.
My best guess is that Visual Studio is encoding the url parameters in an unexpected way. But I can't figure out what it is doing that is different. I also can't see a way to inspect the response contents, which would allow me to dump the data that the server is seeing into the response and debug it. I've tried things like
_ResponseString = _WebClient.UploadString(_Url, _Parameters);
Debug.WriteLine(_ResponseString);
But the exception is thrown before that Debug line, and when I inspect the Exception, the response has no contents.
edit:
I see my mistake, I'm sending the parameters as a query string in my REST client, but as request parameters from Visual Studio. The fix (for this example) is to change the UploadString call to
_WebClient.UploadString(_Url + "?" + _Parameters, "");
Just to make the solution to this more visible I'm going to post my edit as an answer
edit: I see my mistake, I'm sending the parameters as a query string
in my REST client, but as request parameters from Visual Studio. The
fix (for this example) is to change the UploadString call to
_WebClient.UploadString(_Url + "?" + _Parameters, "");
instead of
_WebClient.UploadString(_Url, _Parameters);
I am trying to connect to a web service from Lockheed Martin located here. I have looked at other examples and am using the following code to try and establish a connection. All I want to know at this point is if I have established a connection and been authorized but I repeatedly get an exception saying
Unauthorized at System.Net.HttpWebRequest.GetResponse()
. Am I setting up the web request and response correctly? Is there a different method that would simply let me know if I've successfully connected?
try
{
//Connect to the Lockheed Martin web client
WebRequest client = WebRequest.Create("https://www.elabs.testafss.net/Website2/ws");
string username = "username";
string password = "password";
string credentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(username + ":" + password));
client.Headers.Add("Authorization", "Basic " + credentials);
WebResponse response = client.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Probably your user / password is incorrect, looking at the documentation of the web service, your code is reproducing the exactly same hash that is in following sample:
Authentication - Basic Auth
Authentication is performed using the Basic Auth protocol. An authorization header is supplied with every web service request. This is sometimes called pre-emptive authentication. The header looks like this:
Authorization: Basic Vendor_ID:Vendor_Password
where the Vendor_ID:Vendor_Password string is converted to Base64.
Example
Authorization: Basic JoesFlightServices:SecretPW
Converted to Base64:
Authorization: Basic Sm9lc0ZsaWdodFNlcnZpY2VzOlNlY3JldFBX
Note that conversion to Base64 does not ensure the information will be private. We use HTTPS to encrypt the entire HTTP message including the headers.
Source
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