如何发送消息?
how to send message?
这里sendasync()
功能是怎么用的我也不知道
我使用 foreach
循环然后获取手机号码和消息每次都相同但主要问题是 SendAsync 是如何使用我不知道。
[WebMethod(true)]
public static string SendMessage(List<int> ids, string message)
{
foreach (var id in ids)
{
HttpClient client = new HttpClient();
using (mapsEntityDataContext db = new mapsEntityDataContext())
{
tbl_inq edit = db.tbl_inqs.SingleOrDefault(x => x.Inq_Id == id);
var mobile = edit.Contact;
client.BaseAddress = new Uri("http://sms.hspsms.com/sendSMS?username=hspdemo&message=" + message + "&sendername=HSPSMS&smstype=TRANS&numbers=" + mobile + "&apikey=66e12418-8b67-4c2a-9a08-4fd459bfa84c");
client.SendAsync();
}
//client.SendAsync();
}
return "sucess";
}
有几种方法可以执行 GET 和 POST 请求:
方法一:HttpClient
目前首选的方法。异步。 .NET 4.5 附带;可通过 NuGet 获得的适用于其他平台的便携版本。
使用System.Net.Http;
POST
using (var client = new HttpClient())
{
var values = new Dictionary<string, string>
{
{ "thing1", "hello" },
{ "thing2", "world" }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);
var responseString = await response.Content.ReadAsStringAsync();
}
得到
using (var client = new HttpClient())
{
var responseString = await client.GetStringAsync("http://www.example.com/recepticle.aspx");
}
方法 B:第 3 方库
RestSharp
用于与 REST API 交互的久经考验的库。便携的。可通过 NuGet 获取。
Flurl.Http
较新的库具有流畅的 API 和测试助手。引擎盖下的 HttpClient。便携的。可通过 NuGet 获取。
using Flurl.Http;
POST
var responseString = await "http://www.example.com/recepticle.aspx"
.PostUrlEncodedAsync(new { thing1 = "hello", thing2 = "world" })
.ReceiveString();
得到
var responseString = await "http://www.example.com/recepticle.aspx"
.GetStringAsync();
方法 C:旧版
using System.Net;
using System.Text; // for class Encoding
POST
var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");
var postData = "thing1=hello";
postData += "&thing2=world";
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
得到
var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
方法 D:WebClient(现在也是旧版)
using System.Net;
using System.Collections.Specialized;
POST
using (var client = new WebClient())
{
var values = new NameValueCollection();
values["thing1"] = "hello";
values["thing2"] = "world";
var response = client.UploadValues("http://www.example.com/recepticle.aspx", values);
var responseString = Encoding.Default.GetString(response);
}
得到
using (var client = new WebClient())
{
var responseString = client.DownloadString("http://www.example.com/recepticle.aspx");
}
这里sendasync()
功能是怎么用的我也不知道
我使用 foreach
循环然后获取手机号码和消息每次都相同但主要问题是 SendAsync 是如何使用我不知道。
[WebMethod(true)]
public static string SendMessage(List<int> ids, string message)
{
foreach (var id in ids)
{
HttpClient client = new HttpClient();
using (mapsEntityDataContext db = new mapsEntityDataContext())
{
tbl_inq edit = db.tbl_inqs.SingleOrDefault(x => x.Inq_Id == id);
var mobile = edit.Contact;
client.BaseAddress = new Uri("http://sms.hspsms.com/sendSMS?username=hspdemo&message=" + message + "&sendername=HSPSMS&smstype=TRANS&numbers=" + mobile + "&apikey=66e12418-8b67-4c2a-9a08-4fd459bfa84c");
client.SendAsync();
}
//client.SendAsync();
}
return "sucess";
}
有几种方法可以执行 GET 和 POST 请求:
方法一:HttpClient
目前首选的方法。异步。 .NET 4.5 附带;可通过 NuGet 获得的适用于其他平台的便携版本。
使用System.Net.Http;
POST
using (var client = new HttpClient())
{
var values = new Dictionary<string, string>
{
{ "thing1", "hello" },
{ "thing2", "world" }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);
var responseString = await response.Content.ReadAsStringAsync();
}
得到
using (var client = new HttpClient())
{
var responseString = await client.GetStringAsync("http://www.example.com/recepticle.aspx");
}
方法 B:第 3 方库
RestSharp
用于与 REST API 交互的久经考验的库。便携的。可通过 NuGet 获取。
Flurl.Http
较新的库具有流畅的 API 和测试助手。引擎盖下的 HttpClient。便携的。可通过 NuGet 获取。
using Flurl.Http;
POST
var responseString = await "http://www.example.com/recepticle.aspx"
.PostUrlEncodedAsync(new { thing1 = "hello", thing2 = "world" })
.ReceiveString();
得到
var responseString = await "http://www.example.com/recepticle.aspx"
.GetStringAsync();
方法 C:旧版
using System.Net;
using System.Text; // for class Encoding
POST
var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");
var postData = "thing1=hello";
postData += "&thing2=world";
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
得到
var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
方法 D:WebClient(现在也是旧版)
using System.Net;
using System.Collections.Specialized;
POST
using (var client = new WebClient())
{
var values = new NameValueCollection();
values["thing1"] = "hello";
values["thing2"] = "world";
var response = client.UploadValues("http://www.example.com/recepticle.aspx", values);
var responseString = Encoding.Default.GetString(response);
}
得到
using (var client = new WebClient())
{
var responseString = client.DownloadString("http://www.example.com/recepticle.aspx");
}