这里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";
}
答案 0 :(得分:0)
有多种方法可以执行GET和POST请求:
方法A:logging.level.root=OFF
目前首选方法。异步。运行.NET 4.5;可通过NuGet获得的其他平台的便携版本。
使用HttpClient
POST
System.Net.Http;
}
GET
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();
方法B:第三方库
RestSharp
经过试验和测试的库,用于与REST API交互。便携。可通过NuGet获取。
Flurl.Http
较新的图书馆提供流畅的API和测试助手。引擎盖下的HttpClient。便携。可通过NuGet获取。
using (var client = new HttpClient())
{
var responseString = await client.GetStringAsync("http://www.example.com/recepticle.aspx");
}
POST
using Flurl.Http;
GET
var responseString = await "http://www.example.com/recepticle.aspx"
.PostUrlEncodedAsync(new { thing1 = "hello", thing2 = "world" })
.ReceiveString();
方法C:遗产
var responseString = await "http://www.example.com/recepticle.aspx"
.GetStringAsync();
POST
using System.Net;
using System.Text; // for class Encoding
GET
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();
方法D:WebClient(现在也是遗产)
var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
POST
using System.Net;
using System.Collections.Specialized;
GET
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);
}