WebRequest.GetResponse - 远程服务器返回错误:(404)Not Found

时间:2017-09-07 20:37:31

标签: vb.net webrequest

我正在尝试连接到某个网站,但即使我可以在浏览器中访问该网站,它仍然会返回此错误:

  

System.dll中出现“System.Net.WebException”类型的异常   但未在用户代码中处理

     

其他信息:远程服务器返回错误:(404)不是   找到。

我很确定我的代码是正确的,因为我最近使用了相同的代码,但无法解决为什么它会返回错误,有什么建议吗? 我的代码:

OddsTodayREQUEST = WebRequest.Create("http://www.betexplorer.com/next/soccer/")
Using OddsTodayRESPONSE As WebResponse = OddsTodayREQUEST.GetResponse()
            Using OddsTodayREADER As New StreamReader(OddsTodayRESPONSE.GetResponseStream())
                OddsTodayHTML = OddsTodayREADER.ReadToEnd()
            End Using
        End Using

2 个答案:

答案 0 :(得分:1)

该网站希望将用户代理添加到请求中。你可以谷歌OddsTodayREQUEST.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)" 找到你自己的,并添加如下:

import cv2
import numpy as np

# read and scale down image
img = cv2.pyrDown(cv2.imread('C:\\Users\\Work\\Desktop\\test.png', cv2.IMREAD_UNCHANGED))

# threshold image
ret, threshed_img = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),
                                  127, 255, cv2.THRESH_BINARY)
# find contours and get the external one
image, contours, hier = cv2.findContours(threshed_img, cv2.RETR_TREE,
                                         cv2.CHAIN_APPROX_SIMPLE)

# with each contour, draw boundingRect in green
# a minAreaRect in red and
# a minEnclosingCircle in blue
for c in contours:
    # get the bounding rect
    x, y, w, h = cv2.boundingRect(c)
    # draw a green rectangle to visualize the bounding rect
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), thickness=1, lineType=8, shift=0)

    # get the min area rect
    #rect = cv2.minAreaRect(c)
    #box = cv2.boxPoints(rect)
    # convert all coordinates floating point values to int
    #box = np.int0(box)
    # draw a red 'nghien' rectangle
    #cv2.drawContours(img, [box], 0, (0, 0, 255))

    # finally, get the min enclosing circle
    #(x, y), radius = cv2.minEnclosingCircle(c)
    # convert all values to int
    #center = (int(x), int(y))
    #radius = int(radius)
    # and draw the circle in blue
    #img = cv2.circle(img, center, radius, (255, 0, 0), 2)

print(len(contours))
cv2.drawContours(img, contours, -1, (255, 255, 0), 1)

cv2.namedWindow('contours', 0)
cv2.imshow('contours', img)
while(cv2.waitKey()!=ord('q')):
    continue
cv2.destroyAllWindows()

答案 1 :(得分:0)

你需要添加UserAgent作为@ChaseRocker提到,除了他的答案,最好使用HttpWebClient的AutomaticDecompression属性,你可以添加Accept标头。我还在OddsTodayRESPONSE.GetResponseStream()语句中使用了Using

Dim OddsTodayREQUEST As HttpWebRequest = WebRequest.Create("http://www.betexplorer.com/next/soccer/")
OddsTodayREQUEST.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
OddsTodayREQUEST.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate 'Decompressing makes the request be done faster
OddsTodayREQUEST.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0"
Using OddsTodayRESPONSE As HttpWebResponse = OddsTodayREQUEST.GetResponse()
    Using OddsTodayRESPONSESTREAM = OddsTodayRESPONSE.GetResponseStream()
        Using OddsTodayREADER As New StreamReader(OddsTodayRESPONSESTREAM)
            OddsTodayHTML = OddsTodayREADER.ReadToEnd()
        End Using
    End Using
End Using
相关问题