The remote server returned an error: (403) Forbidden. HttpWebRequest Response Error?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<table>
<tbody>
<tr>
<td>
<span class="x_vote-count-post <br type="_moz

</td>
<td>


So after looking some things up I think I have figured out why I keep getting the following error: "The remote server returned an error: (403) Forbidden."
Here is the code:
<pre> <pre class="prettyprint Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Set the initial parameters
Dim UserID As String = TextBox1.Text Username
Dim PWord As String = TextBox2.Text Password
Dim domain As String = "https://pinterest.com/login/?next=%2F"
Dim encoding As New System.Text.ASCIIEncoding
Dim CookieC As New Net.CookieContainer

Use the appropriate HTML field names to stuff into the post header
Dim PostData As String = _
"email" & UserID & _
"&password=" & PWord Note: where txtUName & txtPWord are the field names
Dim Data() As Byte = encoding.GetBytes(PostData)

Initialise the request
Dim LoginReq As Net.HttpWebRequest = Net.WebRequest.Create(domain) Login location taken from the form action
With LoginReq
.KeepAlive = False
.Method = "POST"
Note: if the page uses a redirect if will fail
.AllowAutoRedirect = False
.ContentType = "application/x-www-form-urlencoded"
.ContentLength = Data.Length
Set empty container
.CookieContainer = CookieC
End With

Add the POST data
Dim SendReq As IO.Stream = LoginReq.GetRequestStream
SendReq.Write(Data, 0, Data.Length)
SendReq.Close()

Obtain the response
Dim LoginRes As Net.HttpWebResponse = LoginReq.GetResponse()

Retreive the headers from the request (e.g. the location header)
Dim Redirect As String = LoginRes.Headers("Location")
Add any returned cookies to the cookie collection
CookieC.Add(LoginRes.Cookies)

Move to the redirected page as a GET request...
LoginReq = Net.WebRequest.Create(domain & Redirect)
With LoginReq
.KeepAlive = False
.Method = "GET"
.ContentType = "application/x-www-form-urlencoded"
.AllowAutoRedirect = True
.CookieContainer = CookieC
End With

Perform the navigate and output the HTML
LoginRes = LoginReq.GetResponse()
Dim sReader As IO.StreamReader = New IO.StreamReader(LoginRes.GetResponseStream)
Dim HTML As String = sReader.ReadToEnd
If HTML.Contains("Following") Then

MessageBox.Show("yay!")

Else

MessageBox.Show("no!")

End If


End Sub[/code]<br/>
[/code]
I added in the following code after the following line: Dim LoginReq As Net.HttpWebRequest = Net.WebRequest.Create(domain)
Code after the line above:
<pre><pre class="prettyprint LoginReq.Credentials = New System.Net.NetworkCredential(UserID, PWord)[/code]<br/>
[/code]
But, the following line is where the issue keeps occuring:
<pre><pre class="prettyprint Dim LoginRes As Net.HttpWebResponse = LoginReq.GetResponse()[/code]<br/>
[/code]
I have no idea how to fix this. Please help!


</td>
</tr>
</tbody>
</table>

View the full article
 
Back
Top