VB.NET短信接口、VB.NET短信验证码接口源码、VB.N

时间:2016-06-13 11:42 来源:原创 作者:admin

VB.NET短信接口、VB.NET短信验证码接口源码、VB.NET发短信、VB短信接口源码

'乱码问题处理:
 '* 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=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
 
  '  示例
 
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