根據IP調用webservice方案
1、創建ClientIPs.xml
<?xml version="1.0" encoding="utf-8" ?>
<IPS>
<IP>127.0.0.</IP>
<IP>10.2.16.</IP>
</IPS>
2、在web.config的添加appSettings標籤中添加 <add key="IPsXml" value="ClientIPs.xml"/>
3、在當前項目中添加web服務,由於時間比較緊,就拿目前在開發的VB項目爲例。C#簡單的
核心代碼如下:
#Region "判斷ip地址"
''' <summary>
''' 判斷是否是允許訪問的客戶端IP地址
''' </summary>
''' <param name="strIP">客戶端IP地址</param>
''' <returns>bool</returns>
''' <remarks></remarks>
Public Function IsAllowedIP(ByVal strIP As String) As Boolean
Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.Load(Server.MapPath(System.Configuration.ConfigurationManager.AppSettings("IPsXml")))
Dim rootNode As XmlNode = xmlDoc.DocumentElement '獲取根節點
For Each node As XmlNode In rootNode.ChildNodes
If node.InnerXml.Substring(node.InnerXml.Length - 1, 1) = "." Then
' 如果是一個網段地址
If strIP.Contains(node.InnerXml) Then
Return True
End If
Else
If strIP = node.InnerXml Then
Return True
End If
End If
Next
Return False
End Function
#End Region
''' <summary>
''' 受限IP得到所有行政許可受理信息
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
<WebMethod(EnableSession:=True, Description:="受限IP得到所有行政許可受理信息")> _
Public Function GetAcceptances() As DataTable
If IsAllowedIP(Me.Context.Request.ServerVariables("REMOTE_ADDR").ToString()) Then
Return GetAcceptance() '調用業務方法
Else
Return Nothing
End If
End Function
webservice註釋:普通的XML 註釋不行,要用特性參數,Description來設置
OK這樣就好了,弄好部署到服務器上
遇到調用的問題:
webservice遠程測試顯示 “測試窗體只能用於來自本地計算機的請求”
The test form is only available for requests from the local machine.
解決的方法:
在web.config中加入如下配置節內容
<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpPost"/>
<add name="HttpGet"/>
<add name="Documentation"/>
</protocols>
</webServices>