调用VB.NET说明
-
普通短信发送
-
用户在遵循HTTP协议的前提下,可通过GET和POST方式提交短信发送请求
-
普通短信发送
-
短信可以提交不超过50000个手机号码,每个号码用英文逗号间隔。 (一次提交发送超过200个手机号码,请使用POST请求)
乱码问题解决方案:
1、GBK编码提交的首先urlencode短信内容(content),然后在API请求时,带入encode=gbk
2、UTF-8编码的将content 做urlencode编码后,带入encode=utf8或utf-8实例:http://m.5c.com.cn/api/send/index.php?username=XXX&password_md5=XXX&apikey=XXX&mobile=XXX&content=%E4%BD%A0%E5%A5%BD%E6%89%8D%E6%94%B6%E7%9B%8A%E9%9F%A6&encode=utf8
内容转码问题解决方案:
1、 UTF-8 转 GBK:$content = iconv("UTF-8","GBK//IGNORE",$content);
2、 GBK 转 UTF-8:$content = iconv("GBK","UTF-8",$content);
发送接口返回结果
序号 |
参数 |
说明 |
1 |
success:msgid |
提交成功 |
2 |
error:Missing username |
用户名为空 |
3 |
error:Missing password |
密码为空 |
4 |
error:Missing apikey |
APIKEY为空 |
5 |
error:Missing recipient |
手机号为空 |
6 |
error:Missing message content |
短信内容为空 |
7 |
error:Account is blocked |
账号被禁用 |
8 |
error:Unrecognized encoding |
编码未能识别 |
9 |
error:APIKEY or password error |
APIKEY或密码错误 |
10 |
error:Unauthorized IP address |
未授权 IP 地址 |
11 |
error:Account balance is insufficient |
余额不足 |
VB.NET代码示例:
Module Module1
Sub Main()
Dim encode As String = "UTF-8" '页面编码和短信内容编码为GBK。重要说明:如提交短信后收到乱码,请将GBK改为UTF-8测试。如本程序页面为编码格式为:ASCII/GB2312/GBK则该处为GBK。如本页面编码为UTF-8或需要支持繁体,阿拉伯文等Unicode,请将此处写为:UTF-8
Dim username As String = "" '用户名
Dim password As String = "" '密码
Dim mobile As String = "" '手机号,只发一个号码:13800000001。发多个号码:13800000001,13800000002,...N 。使用半角逗号分隔。
Dim apikey As String = "" 'apikey秘钥(请登录 http://m.5c.com.cn 短信平台-->账号管理-->我的信息 中复制apikey)
Dim content As String = "美联软通VB示例【美联】" '要发送的短信内容,特别注意:签名必须设置,网页验证码应用需要加添加【图形识别码】。
Dim content1 As String = URLEncode(content)
'POST 方式
Dim sbTemp As System.Text.StringBuilder = New System.Text.StringBuilder()
'发送链接(用户名,密码,手机号,apikey,短信内容,编码格式)
sbTemp.Append("username=" + username + "&password=" + password + "&mobile=" + mobile + "&apikey=" + apikey + "&content=" + content1 + "&encode=" + encode)
'对短信内容做Urlencode编码操作。
Dim bTemp() As Byte = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(sbTemp.ToString())
'发送返回的结果存入result中
Dim result As String = PostRequest("http://m.5c.com.cn/api/send/?", bTemp)
'输出result内容,查看返回值,成功为success,错误为error,详见该文档起始注释
Console.WriteLine(result)
End Sub
'发送HTTP POST请求得结果
Private Function PostRequest(ByVal url As String, ByVal bData() As Byte) As String
Dim strReturn As String = ""
Dim hwRequest As System.Net.WebRequest
Dim hwResponse As System.Net.WebResponse
Try
'获取上面的URL链接
hwRequest = System.Net.HttpWebRequest.Create(url)
'设置超时时间
hwRequest.Timeout = 5000
'发送请求为POST
hwRequest.Method = "POST"
hwRequest.ContentType = "application/x-www-form-urlencoded"
'bData的长度(也就是获取用户名,密码,手机号,apikey,短信内容,编码格式总长度)
hwRequest.ContentLength = bData.Length
'发送
Dim smWrite As System.IO.Stream = hwRequest.GetRequestStream()
smWrite.Write(bData, 0, bData.Length)
smWrite.Close()
'使用hwResponse来获取数据
hwResponse = hwRequest.GetResponse()
Dim srReader As System.IO.StreamReader = New System.IO.StreamReader(hwResponse.GetResponseStream(), System.Text.Encoding.ASCII)
strReturn = srReader.ReadToEnd()
srReader.Close()
hwResponse.Close()
Catch
End Try
Return strReturn
End Function
Public Function URLEncode(ByVal strParameter As String) As String
Dim s As String
Dim I As Integer
Dim intValue As Integer
Dim TmpData() As Byte
s = ""
TmpData = StrConv(strParameter, vbFromUnicode)
For I = 0 To UBound(TmpData)
intValue = TmpData(I)
If (intValue >= 48 And intValue <= 57) Or _
(intValue >= 65 And intValue <= 90) Or _
(intValue >= 97 And intValue <= 122) Then
s = s & Chr(intValue)
ElseIf intValue = 32 Then
s = s & "+"
Else
s = s & "%" & Hex(intValue)
End If
Next I
URLEncode = s
End Function
End Module