在WebMethod中使用HttpWebRequest对象

时间:2014-01-06 14:36:48

标签: asp.net httpwebrequest asmx webmethod

我试图在HttpWebRequest中使用Webmethod对象,但我得到设计时错误Type 'HttpWebRequest' is not defined.

我该如何解决这个问题?

<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<ToolboxItem(False)> _
Public Class geolocation
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function GetCities(ByVal prefixText As String, ByVal contextKey As String) As String()

    Dim req As HttpWebRequest = HttpWebRequest.Create("http://www.mytesturl.com")
    'on this line I get: Type 'HttpWebRequest' is not defined (first HttpWebRequest)

    End Function

End Class

1 个答案:

答案 0 :(得分:0)

尝试从给定网址创建网络请求:

Dim sUri As String = "yourURL"
Dim myUri As System.Uri = New System.Uri(sUri)
Dim HttpWRequest As HttpWebRequest = WebRequest.Create(myUri)

编辑: 并且不要忘记在项目中添加Namespace的引用:System.Net Assembly:System(在System.dll中)