Getting 403 Forbidden error on a valid public web page

jbl_ks

Member
Joined
Dec 16, 2010
Messages
24
Programming Experience
Beginner
When I run this and click the button I get an error message "The remote server returned an error: (403) Forbidden."

I can paste that same url in a browser window and it will open the page (it is actually a 256x256px png image)

Using Perl I can use the (LWP-Get/Store) function on that url and it will also get the image.

I would like to get this working in VB.NET (2010)
(The extra namespaces are for other methods)

Imports System
Imports System.Windows.Forms
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing.Imaging
Imports System.Drawing
Imports System.Net
Imports System.IO


Public Class Form1
Private Sub btnDownload_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDownload.Click

Dim webClient As New System.Net.WebClient
Dim strURL As String
Dim strPath As String

strURL = "http://mt1.google.com/vt/lyrs=m@141&hl= _
en&src=api&x=31095&s=&y=50042&z=17&s=Galileo"

strPath = "C:\___4Tiles\31095_50042_Z17_1x1.png"

webClient.DownloadFile(strURL, strPath)

End Sub
End Class
lyrs=m@141&hl=en&src=api&x=31095&s=&y=50042&z=17&s=Galileo
 
The most common reason is the web server requires the UserAgent header to be set in request, add this:
VB.NET:
webClient.Headers.Add(Net.HttpRequestHeader.UserAgent, ".Net client")
As a general advice when encountering problems like this, inspect the request your regular browser client makes when you navigate to a page, to see what it sends. Fiddler is one tool I know of that may be used to debug the requests (and responses).
 
Thanks a lot JohnH, this works perfectly now.

Imports System.Net

Public Class Form1

Private Sub btnGetTiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetTiles.Click
Dim webClient As New System.Net.WebClient
Dim strURL As String
Dim strPath As String
webClient.Headers.Add(Net.HttpRequestHeader.UserAgent, ".Net client")
strURL = "http://mt1.google.com/vt/lyrs=m@141&hl=en&src=api&x=31095&s=&y=50042&z=17&s=Galileo"

strPath = "C:\___4Tiles\31095_50042_Z17_1x1_02-28-2010_v3.png"

webClient.DownloadFile(strURL, strPath)
End Sub
End Class
 
Back
Top