简体   繁体   English

如何发送信息?

[英]how to send message?

Here sendasync() function is how to use I don't know. 这里不知道sendasync()函数是如何使用的。

I use foreach loop then get mobile number and message is same every time but main problem is SendAsync is how to use I don't know . 我使用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";
}

There are several ways to perform GET and POST requests: 有几种执行GET和POST请求的方法:

Method A: HttpClient 方法A: HttpClient

Currently the preferred approach. 目前是首选方法。 Asynchronous. 异步。 Ships with .NET 4.5; 附带.NET 4.5; portable version for other platforms available via NuGet. 可通过NuGet获得的其他平台的便携式版本。

using System.Net.Http; 使用System.Net.Http; POST 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();

} }

GET 得到

  using (var client = new HttpClient())
{
    var responseString = await   client.GetStringAsync("http://www.example.com/recepticle.aspx");
}

Method B: 3rd-Party Libraries 方法B:第三方库

RestSharp RestSharp

Tried and tested library for interacting with REST APIs. 经过测试的库,用于与REST API进行交互。 Portable. 便携。 Available via NuGet. 可通过NuGet获得。

Flurl.Http Flurl.Http

Newer library sporting a fluent API and testing helpers. 较新的库具有流畅的API和测试助手。 HttpClient under the hood. 深入了解HttpClient。 Portable. 便携。 Available via NuGet. 可通过NuGet获得。

using Flurl.Http;

POST POST

var responseString = await "http://www.example.com/recepticle.aspx"
    .PostUrlEncodedAsync(new { thing1 = "hello", thing2 = "world" })
    .ReceiveString();

GET 得到

var responseString = await "http://www.example.com/recepticle.aspx"
    .GetStringAsync();

Method C: Legacy 方法C:旧版

using System.Net;
using System.Text;  // for class Encoding

POST 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();

GET 得到

var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");

var response = (HttpWebResponse)request.GetResponse();

var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

Method D: WebClient (Also now legacy) 方法D:WebClient(现在也已保留)

using System.Net;
using System.Collections.Specialized;

POST 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);
}

GET 得到

using (var client = new WebClient())
{
    var responseString = client.DownloadString("http://www.example.com/recepticle.aspx");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM