VB.NET远程服务器返回错误:(405)Method Not Allowed

时间:2015-12-02 14:10:15

标签: vb.net web-services

以下代码适用于某些网址,但其他一些有参数的网址会返回错误:The remote server returned an error: (405) Method Not Allowed.

我的工作:

        Dim objHttpWebRequest As HttpWebRequest = Nothing
        Dim objHttpWebResponse As HttpWebResponse = Nothing
        Dim objRequestStream As Stream = Nothing
        Dim objResponseStream As Stream = Nothing
        Dim objXMLReader As XmlTextReader

        Try
        objHttpWebRequest = WebRequest.Create(URL)

        'Start HttpRequest
        objHttpWebRequest.Method = "POST"
        objHttpWebRequest.ContentType = "application/xml"

        'Get Stream Object
        objRequestStream = objHttpWebRequest.GetRequestStream()
        objRequestStream.Close()

        'Start HTTP Response
        objHttpWebResponse = objHttpWebRequest.GetResponse()

        If objHttpWebResponse.StatusCode = HttpStatusCode.OK Then
            objResponseStream = objHttpWebResponse.GetResponseStream()
            objXMLReader = New XmlTextReader(objResponseStream)

            Dim xmldoc As XmlDocument = New XmlDocument
            xmldoc.Load(objXMLReader)
            XMLResponse = xmldoc
            objXMLReader.Close()
        End If

我正在使用的方法中存在问题吗?还是内容类型?

1 个答案:

答案 0 :(得分:1)

根据状态代码,问题出在方法中。并非所有网址都可以响应POST个请求。

Wikipedia

  

405方法不允许

     

使用不支持的请求方法请求资源   通过该资源;例如,在需要数据的表单上使用GET   通过POST呈现,或在只读资源上使用PUT。