Foxtable(狐表)用户栏目专家坐堂 → 关于webservers发送短信的问题


  共有3850人关注过本帖树形打印复制链接

主题:关于webservers发送短信的问题

帅哥哟,离线,有人找我吗?
chenjiu6202
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:403 积分:3892 威望:0 精华:0 注册:2013/1/18 21:38:00
关于webservers发送短信的问题  发帖心情 Post By:2016/1/9 14:24:00 [显示全部帖子]

我想利用webservers接口进行第三方短信发送,
厂家给我发来一个API说明和Asp.net的一个实例,一头雾水不知从何下手,
诚恳请哪位老师给我说一说,帮帮我!先表示感谢!下面是那个实例的一部分
<%
url = "http://service2.winic.org/Service.asmx"  'webservice 地址

'==============================查询账户信息
function GetUserInfo(uid,pwd)
SoapRequest="<?xml version="&CHR(34)&"1.0"&CHR(34)&" encoding="&CHR(34)&"utf-8"&CHR(34)&"?>"& _
"<soap:Envelope xmlns:xsi="&CHR(34)&"http://www.w3.org/2001/XMLSchema-instance"&CHR(34)&" "& _
"xmlns:xsd="&CHR(34)&"http://www.w3.org/2001/XMLSchema"&CHR(34)&" "& _
"xmlns:soap="&CHR(34)&"http://schemas.xmlsoap.org/soap/envelope/"&CHR(34)&">"& _
"<soap:Body>"& _
"<GetUserInfo xmlns="&CHR(34)&"http://tempuri.org/"&CHR(34)&">"& _
"<uid>"&uid&"</uid>"& _
"<pwd>"&pwd&"</pwd>"& _
"</GetUserInfo>"& _
"</soap:Body>"& _
"</soap:Envelope>"

Set xmlhttp = server.CreateObject("Msxml2.XMLHTTP")
xmlhttp.Open "POST",url,false
xmlhttp.setRequestHeader "Content-Type", "text/xml;charset=utf-8"
xmlhttp.setRequestHeader "HOST","service2.winic.org"
xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest)
xmlhttp.setRequestHeader "SOAPAction", "http://tempuri.org/GetUserInfo" '一定要与WEBSERVICE的命名空间相同,否则服务会拒绝
xmlhttp.Send(SoapRequest)
''样就利用XMLHTTP成功发送了与SOAP示例所符的SOAP请求.'检测一下是否返回200=成功: 
     If xmlhttp.Status = 200 Then
        Set xmlDOC = server.CreateObject("MSXML.DOMDocument")
        xmlDOC.load(xmlhttp.responseXML)
            GetUserInfo=xmlDOC.documentElement.selectNodes("//GetUserInfoResult")(0).text '显示节点为GetUserInfoResult的数据(返回字符串)
        Set xmlDOC = nothing
    Else
        GetUserInfo=xmlhttp.Status&"&nbsp;"
        GetUserInfo=xmlhttp.StatusText
    End if
        Set xmlhttp = Nothing
end function

 回到顶部
帅哥哟,离线,有人找我吗?
chenjiu6202
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:403 积分:3892 威望:0 精华:0 注册:2013/1/18 21:38:00
  发帖心情 Post By:2016/1/20 18:07:00 [显示全部帖子]


图片点击可在新窗口打开查看此主题相关图片如下:qq图片20160120180005.png
图片点击可在新窗口打开查看

图片点击可在新窗口打开查看此主题相关图片如下:qq图片20160120180041.png
图片点击可在新窗口打开查看

图片点击可在新窗口打开查看此主题相关图片如下:qq图片20160120180152.png
图片点击可在新窗口打开查看

 

这三张图片是厂家的API说明,http://10.1.1.10/API/BS3Webservice.asmx是访问地址,有用户名和密码

用post和get都不能成功发短信,也许要用soap调用,哪位老师帮忙给看看,怎么样才能发出去短信,谢谢!


 回到顶部
帅哥哟,离线,有人找我吗?
chenjiu6202
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:403 积分:3892 威望:0 精华:0 注册:2013/1/18 21:38:00
  发帖心情 Post By:2016/1/21 18:46:00 [显示全部帖子]

袍哥,用你给的例子,无论是post还是get都是只打开一个网页,用户名和密码等都用的

是Api的说明,你看看下面代码哪里错误了啊,第一个用get

Dim url  As  String
Dim http As Object
url =  "http://10.1.1.10/API/BS3Webservice.asmx?UserName=ytzdtlxxzx&UserPsw=123456&SMSMobile=13734562406&SMSC"
http = CreateObject("MSXML2.XMLHTTP")
http.Open("get", url, False)
http.send()
MessageBox.Show("返回信息:" & http.responseText)

 

第二个用post

 

Dim url As String = "http://10.1.1.10/API/BS3Webservice.asmx?UserName=ytzdtlxxzx&UserPsw=123456&SMSMobile=13734562406&SMSC"

Dim rqst As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(url)

Dim rsps As System.Net.HttpWebResponse = rqst.GetResponse

Dim stm As System.IO.Stream = rsps.GetResponseStream()

Dim reader As New System.IO.StreamReader(stm)

Dim str As String = reader.ReadToEnd

rsps.Close
stm.Close
reader.close

msgbox(str)

袍哥帮忙给看看,究竟怎么回事啊

 


 回到顶部
帅哥哟,离线,有人找我吗?
chenjiu6202
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:403 积分:3892 威望:0 精华:0 注册:2013/1/18 21:38:00
  发帖心情 Post By:2016/1/22 8:59:00 [显示全部帖子]

Dim url  As  String
Dim http As Object
url =  "http://10.1.1.10/API/BS3Webservice.asmx/GetBalance?UserName=ytzdtlxxzx&UserPsw=123456"
http = CreateObject("MSXML2.XMLHTTP")
http.Open("get", url, False)
http.send()
MessageBox.Show("返回信息:" & http.responseText)

这是我的代码,连接后返回信息为下面这张图片

 


图片点击可在新窗口打开查看此主题相关图片如下:qq图片20160122085244.jpg
图片点击可在新窗口打开查看

 回到顶部
帅哥哟,离线,有人找我吗?
chenjiu6202
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:403 积分:3892 威望:0 精华:0 注册:2013/1/18 21:38:00
  发帖心情 Post By:2016/1/22 9:02:00 [显示全部帖子]

他的网页里面应该加入

web.config文件中的 <system.web> 节点下加入:
<webServices>
    <protocols>
        <add name= "HttpPost"/>
        <add name= "HttpGet"/>
    </protocols>
</webServices>

这样我才能支持get和post调用,但是现在get和post调用都不行,应该只能用soap啊

袍哥,有更好的办法一定要帮帮我啊


 回到顶部
帅哥哟,离线,有人找我吗?
chenjiu6202
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:403 积分:3892 威望:0 精华:0 注册:2013/1/18 21:38:00
  发帖心情 Post By:2016/2/26 17:12:00 [显示全部帖子]

Imports System.Web
Imports System.Xml
Imports System.Collections
Imports System.Net
Imports System.Text
Imports System.IO
Imports System.Xml.Serialization

'By huangz 2008-3-19

'
''' <summary>
'''  利用WebRequest/WebResponse进行WebService调用的类,By 同济黄正 http://hz932.ys168.com 2008-3-19
''' </summary>
Public Class WebSvcCaller
 '<webServices>
 '  <protocols>
 '    <add name="HttpGet"/>
 '    <add name="HttpPost"/>
 '  </protocols>
 '</webServices>

 Private Shared _xmlNamespaces As New Hashtable()
 '缓存xmlNamespace,避免重复调用GetNamespace
 '
 ''' <summary>
 ''' 需要WebService支持Post调用
 ''' </summary>
 Public Shared Function QueryPostWebService(URL As [String], MethodName As [String], Pars As Hashtable) As XmlDocument
  Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(URL & "/" & MethodName), HttpWebRequest)
  request.Method = "POST"
  request.C
  SetWebRequest(request)
  Dim data As Byte() = EncodePars(Pars)
  WriteRequestData(request, data)

  Return ReadXmlResponse(request.GetResponse())
 End Function
 '
 ''' <summary>
 ''' 需要WebService支持Get调用
 ''' </summary>
 Public Shared Function QueryGetWebService(URL As [String], MethodName As [String], Pars As Hashtable) As XmlDocument
  Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(URL & "/" & MethodName & "?" & ParsToString(Pars)), HttpWebRequest)
  request.Method = "GET"
  request.C
  SetWebRequest(request)
  Return ReadXmlResponse(request.GetResponse())
 End Function

 


 '
 ''' <summary>
 ''' 通用WebService调用(Soap),参数Pars为String类型的参数名?参数值
 ''' </summary>
 Public Shared Function QuerySoapWebService(URL As [String], MethodName As [String], Pars As Hashtable) As XmlDocument
  If _xmlNamespaces.ContainsKey(URL) Then
   Return QuerySoapWebService(URL, MethodName, Pars, _xmlNamespaces(URL).ToString())
  Else
   Return QuerySoapWebService(URL, MethodName, Pars, GetNamespace(URL))
  End If
 End Function

 

 Private Shared Function QuerySoapWebService(URL As [String], MethodName As [String], Pars As Hashtable, XmlNs As String) As XmlDocument
  'By 同济黄正 http://hz932.ys168.com 2008-3-19
  _xmlNamespaces(URL) = XmlNs
  '加入缓存,提高效率
  Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(URL), HttpWebRequest)
  request.Method = "POST"
  request.C
  request.Headers.Add("SOAPAction", """" & XmlNs & (If(XmlNs.EndsWith("/"), "", "/")) & MethodName & """")
  SetWebRequest(request)
  Dim data As Byte() = EncodeParsToSoap(Pars, XmlNs, MethodName)
  WriteRequestData(request, data)
  Dim doc As New XmlDocument(), doc2 As New XmlDocument()
  doc = ReadXmlResponse(request.GetResponse())

 

  Dim mgr As New XmlNamespaceManager(doc.NameTable)
  mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/")
  Dim RetXml As [String] = doc.SelectSingleNode("//soap:Body/*/*", mgr).InnerXml
  doc2.LoadXml("<root>" & RetXml & "</root>")
  AddDelaration(doc2)
  Return doc2
 End Function
 Private Shared Function GetNamespace(URL As [String]) As String
  Dim request As HttpWebRequest = DirectCast(WebRequest.Create(URL & "?WSDL"), HttpWebRequest)
  SetWebRequest(request)
  Dim response As WebResponse = request.GetResponse()
  Dim sr As New StreamReader(response.GetResponseStream(), Encoding.UTF8)
  Dim doc As New XmlDocument()
  doc.LoadXml(sr.ReadToEnd())
  sr.Close()
  Return doc.SelectSingleNode("//@targetNamespace").Value
 End Function
 Private Shared Function EncodeParsToSoap(Pars As Hashtable, XmlNs As [String], MethodName As [String]) As Byte()
  Dim doc As New XmlDocument()
  doc.LoadXml("<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""></soap:Envelope>")
  AddDelaration(doc)
  Dim soapBody As XmlElement = doc.CreateElement("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/")
  Dim soapMethod As XmlElement = doc.CreateElement(MethodName)
  soapMethod.SetAttribute("xmlns", XmlNs)
  For Each k As String In Pars.Keys
   Dim soapPar As XmlElement = doc.CreateElement(k)
   soapPar.InnerXml = ObjectToSoapXml(Pars(k))
   soapMethod.AppendChild(soapPar)
  Next
  soapBody.AppendChild(soapMethod)
  doc.DocumentElement.AppendChild(soapBody)
  Return Encoding.UTF8.GetBytes(doc.OuterXml)
 End Function
 Private Shared Function ObjectToSoapXml(o As Object) As String
  Dim mySerializer As New XmlSerializer(o.[Gettype]())
  Dim ms As New MemoryStream()
  mySerializer.Serialize(ms, o)
  Dim doc As New XmlDocument()
  doc.LoadXml(Encoding.UTF8.GetString(ms.ToArray()))
  If doc.DocumentElement IsNot Nothing Then
   Return doc.DocumentElement.InnerXml
  Else
   Return o.ToString()
  End If
 End Function
 Private Shared Sub SetWebRequest(request As HttpWebRequest)
  request.Credentials = CredentialCache.DefaultCredentials
  request.Timeout = 10000
 End Sub

 

 Private Shared Sub WriteRequestData(request As HttpWebRequest, data As Byte())
  request.ContentLength = data.Length
  Dim writer As Stream = request.GetRequestStream()
  writer.Write(data, 0, data.Length)
  writer.Close()
 End Sub

 

 Private Shared Function EncodePars(Pars As Hashtable) As Byte()
  Return Encoding.UTF8.GetBytes(ParsToString(Pars))
 End Function

 

 Private Shared Function ParsToString(Pars As Hashtable) As [String]
  Dim sb As New StringBuilder()
  For Each k As String In Pars.Keys
   If sb.Length > 0 Then
    sb.Append("&")
   End If
   sb.Append(HttpUtility.UrlEncode(k) & "=" & HttpUtility.UrlEncode(Pars(k).ToString()))
  Next
  Return sb.ToString()
 End Function

 

 Private Shared Function ReadXmlResponse(response As WebResponse) As XmlDocument
  Dim sr As New StreamReader(response.GetResponseStream(), Encoding.UTF8)
  Dim retXml As [String] = sr.ReadToEnd()
  sr.Close()
  Dim doc As New XmlDocument()
  doc.LoadXml(retXml)
  Return doc
 End Function

 

 Private Shared Sub AddDelaration(doc As XmlDocument)
  Dim decl As XmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", Nothing)
  doc.InsertBefore(decl, doc.DocumentElement)
 End Sub
End Class

袍哥按照你说的,我将c#转vb.net代码后,复制到全局代码中,红色的部分报错,我应该怎么改一下呢?

急需得到你的指点啊,要不晚上睡不好觉觉啊


 回到顶部