public string Send() { using (var handler = new HttpClientHandler() { Credentials = new NetworkCredential(ShortCode.GetUsername(), ShortCode.HashPassword(ShortCode.GetSpID() + ShortCode.GetPassword() + TimeStamp)) }) using (var client = new HttpClient(handler)) { client.BaseAddress = new Uri("http://192.168.9.177:8310"); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "/SendSmsService/services/SendSms/"); request.Content = new StringContent(buildSMSXML(), Encoding.UTF8, "text/xml"); string requestContentString = request.Content.ReadAsStringAsync().Result; var result = client.SendAsync(request).Result; string resultContent = result.Content.ReadAsStringAsync().Result; // Log SendSMS Request. // Useful for debugging multiple delivery notifications for the same request //using (var db = new ApplicationDbContext()) //{ // var webRequest = new Models.WebRequest() // { // Type = "SendSMS", // Content = requestContentString, // Timestamp = DateTime.Now // }; // db.WebRequests.Add(webRequest); // db.SaveChanges(); //} return(resultContent); } }
public static void SendMany(IEnumerable <ScheduledTip> tips) { string currentTimestampString = DateTime.Now.ToString("yyyyMMddHHmmss"); List <HttpRequestMessage> requests = new List <HttpRequestMessage>(); using (var handler = new HttpClientHandler() { Credentials = new NetworkCredential(ShortCode.GetUsername(), ShortCode.HashPassword(ShortCode.GetSpID() + ShortCode.GetPassword() + currentTimestampString)) }) using (var client = new HttpClient(handler)) { client.BaseAddress = new Uri("http://192.168.9.177:8310"); foreach (var tip in tips) { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "/SendSmsService/services/SendSms/"); var message = new Message(tip); request.Content = new StringContent(message.buildSMSXML(timestampString: currentTimestampString), Encoding.UTF8, "text/xml"); //string requestContentString = request.Content.ReadAsStringAsync().Result; requests.Add(request); } //Task[] sendRequests = (from request in requests // select Task.Run(async () => // { // await client.SendAsync(request); // })).ToArray(); //Task.WaitAll(sendRequests, -1); var taskList = new List <Task>(); foreach (var request in requests) { taskList.Add(client.SendAsync(request)); } try { Task.WaitAll(taskList.ToArray()); } catch (Exception) { ; } } }