Sending SMS using clickatell in ASP.Net - c#

I have tried this code from their site
using System.Net;
using System.IO;
WebClient client = new WebClient();
// Add a user agent header in case the requested URI contains a query.
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
client.QueryString.Add("user", "xxxx");
client.QueryString.Add("password", "xxxx");
client.QueryString.Add("api_id", "xxxx");
client.QueryString.Add("to", "xxxx");
client.QueryString.Add("text", "This is an example message");
string baseurl = "http://api.clickatell.com/http/sendmsg";
Stream data = client.OpenRead(baseurl);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
data.Close();
reader.Close();
return s;
I am getting an Proxy authentication failure in
string baseurl ="http://api.clickatell.com/http/sendmsg";
Can anyone help me out??

I think you are behind a proxy and your web client needs to authenticate. Try the following code:
string userName = "<user name>";
string password = "<password>";
ICredentials creds = new NetworkCredential(userName, password);
client.Credentials = creds;

Related

How can i download an excel file from sharepoint server?

I have a project (c# console application), in which I want to automatically download an excel file, via URL, with login credentials.
I have been using a webclient to download the file automatically, and all I receive is an html page as response, informing me to login into the site (I have run it in Chrome.)
private static string url = "[the whole url link to the file, deleted for privacy]";
public void test()
{
const string username = "[mail]";
const string password = "[password]";
var securedPassword = new SecureString();
foreach (var c in password.ToCharArray())
{
securedPassword.AppendChar(c);
}
var credentials = new SharePointOnlineCredentials(username, securedPassword);
DownloadFile(url, credentials, #"C:\Documents\ExcelFilesSinc\test.xlsx");
}
private static void DownloadFile(string webUrl, ICredentials credentials, string fileRelativeUrl)
{
using (var client = new WebClient())
{
client.Credentials = credentials;
string _UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
client.Headers.Add(HttpRequestHeader.UserAgent, _UserAgent);
client.DownloadFile(webUrl, fileRelativeUrl);
}
}
In this way, the file is generated, but the whole content saved in it is an HTML page that requires me to login to Microsoft. Any suggestions?
You can try to add headers for base auth like
client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
client.Headers.Add("User-Agent: Other");
client.Credentials = credentials;
see also here:
Download File From SharePoint 365

C# download image from src without extension

I would like to download an image from this url http://squirlytraffic.com/surficon.php?ts=1491591235
I tried this code but I don't see the image when I open it.
using (WebClient client = new WebClient())
{
client.DownloadFile("http://squirlytraffic.com/surficon.php?ts=1491591235", #"D:\image.jpg");
}
You need to set your credentials using the the WebClient Credentials property. You can do this by assigning it an instance of NetworkCredential. See below:
using (WebClient client = new WebClient()){
client.Credentials = new NetworkCredential("user-name", "password");
client.DownloadFile("url", #"file-location");
}
EDIT
If you don't want to hard code a username and password, you can set the UseDefaultCredentials property of the WebClient to true. This will use the credentials of the currently logged in user. From the documentation.
The Credentials property contains the authentication credentials used to access a resource on a host. In most client-side scenarios, you should use the DefaultCredentials, which are the credentials of the currently logged on user. To do this, set the UseDefaultCredentials property to true instead of setting this property.
Which would mean you could amend the above code to:
using (WebClient client = new WebClient()){
client.UseDefaultCredentials = true;
client.DownloadFile("url", #"file-location");
}
Try this way, those parameters are passed when login
StringBuilder postData = new StringBuilder();
postData.Append("login=" + HttpUtility.UrlEncode("username") + "&");
postData.Append("password=" + HttpUtility.UrlEncode("password") + "&");
postData.Append("Submit=" + HttpUtility.UrlEncode("Login"));
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] postBytes = ascii.GetBytes(postData.ToString());
CookieContainer cc = new CookieContainer();
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create("http://squirlytraffic.com/members.php");
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.ContentLength = postBytes.Length;
webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
webReq.CookieContainer = cc;
Stream postStream = webReq.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Flush();
postStream.Close();
HttpWebResponse res = (HttpWebResponse)webReq.GetResponse();
HttpWebRequest ImgReq = (HttpWebRequest)WebRequest.Create("http://squirlytraffic.com/surficon.php?ts=1491591235");
ImgReq.Method = "GET";
ImgReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
ImgReq.CookieContainer = cc;
HttpWebResponse ImgRes = (HttpWebResponse)ImgReq.GetResponse();
Stream Img = ImgRes.GetResponseStream();

How to query Mixpanel from C#

I am pretty new to web access from C# and completely new to Mixpanel. I am trying to run a query with this code:
using (WebClient wc = new WebClient())
{
wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
byte[] creds = UTF8Encoding.UTF8.GetBytes("<my API secret>:");
wc.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(creds));
var reqparm = new System.Collections.Specialized.NameValueCollection();
reqparm.Add("script", "function main() { return Events({\"from_date\":\"2016-10-01\",\"to_date\":\"2016-10-167\"}).reduce(mixpanel.reducer.count()); }");
byte[] responsebytes = wc.UploadValues("https://mixpanel.com/api/2.0/jql", "POST", reqparm);
}
The query is taken directly from this Mixpanel sample:
function main()
{
return Events
({
from_date: "2016-01-04",
to_date: "2016-01-04"
}).reduce(mixpanel.reducer.count());
}
I've tried lots of variations on the above, but UploadValues always returns 400 (Bad Request). What am I doing wrong?
TIA
There was an error in one of the dates that I didn't notice ("2016-10-167").

Downloading files from Web using basic URL Authorization

I can successfully download a file using the following link in a browser:
https://userbob:qwerty#assoc-datafeeds-na.amazon.com/datafeed/getReport?filename=feedreport-1265.xml.gz
Now I need to to do this in my C# application.
Solution 1: using WebClient
string url = $"https://assoc-datafeeds-na.amazon.com/datafeed/getReport?filename=feedreport-1265.xml.gz";
using (var webClient = new WebClient())
{
string authInfo =Convert.ToBase64String(Encoding.Default.GetBytes("userbob:qwerty"));
webClient.Headers[HttpRequestHeader.Authorization] = "Basic " + authInfo;
webClient.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
webClient.Headers[HttpRequestHeader.UserAgent] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
var data = webClient.DownloadData(url);
}
DownloadData throws an exception: The remote server returned an error: (500) Internal Server Error.
Solution 2: using WebRequest
var request = WebRequest.Create(url);
string authInfo =Convert.ToBase64String(Encoding.Default.GetBytes("userbob:qwerty"));
((HttpWebRequest)request).CookieContainer = new CookieContainer();
request.Headers["Authorization"] = "Basic " + authInfo;
((HttpWebRequest)request).UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
((HttpWebRequest)request).ContentType = "application/octet-stream";
var response = request.GetResponse();
The same exception on the last line. I feel I am missing some header or setting to fully mimic browser behavior.
Interestingly, if I omit Authorization header in either of the solutions, I get 401:Unauthorized exceptions. That makes me think that I pass authorization and the problem is in something else. What am I missing ?
You have to pass token by converting to Base64:
var username = "xxxxxxx";
var password = "yyyyyyy";
var token = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(username + ":" + password));
request.Headers.Add("Authorization", "Basic " + token);

How to send HTTPS request from login box in C#

I am using c#, Microsoft AjaxToolKit.
I am using ajaxToolkit:ModalPopupExtender for the login page, now what I am trying that in my login page it should sent the HTTPS Request POST for the authentication of logging user. Below is the snapshot of my code on the click of login button.
protected void LoginBtn_Click(object sender, EventArgs e)
{
//Add your DB Authentication Module here....
//This is just for testing
if (loginId.Text.Equals("user") && pwd.Text.Equals("user"))
successLabel.Text = "Welcome User";
else
successLabel.Text = "Authentication Failed...Retry";
successLabel.Visible = true;
Loginlnk.Visible = false;
Signuplnk.Visible = true;
}
The above code is just for testing purpose, please suggest how can I proceed to have POST HTTPS WEB REQUEST to authenticate the valid user.
Thanks.
string username = "user";
string password = "pass";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.yoursite.com");
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)";
request.Method = "POST";
using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))
{
writer.Write("nick=" + username + "&password=" + password);
}
Here is what works for me:
var request = WebRequest.Create(_url);
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential(_userName, _password);

Categories