0

I'm developing some API for testing, and I have a problem when I make a webrequest and especially when i retrieve the webresponse. I use this code:

string request = HttpPost("http://iunlocker.net/check_imei.php", "ime_i=013270000134001");


  public static string HttpPost(string URI, string Parameters)
        {
            try
            {
            System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST"; 
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
            req.ContentLength = bytes.Length;
            System.IO.Stream os = req.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);
            os.Close();

            System.Net.WebResponse resp= req.GetResponse();

            if (resp == null) return null;
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            return sr.ReadToEnd().Trim();
            }
            catch (Exception ex) { }
            return null;    
        }

The website in the call is an example, because with this and with other websites I can't retrieve the result correctly. I receive an exception "Error 403"

Can anyone maybe help me by telling what I may be doing wrong?

I thought the problem was on encoding/decoding -- in fact using Fiddler it asks me if I want to decode before see the text -- but with another website, used for examples, I receive the same message from Fiddler but I can retrieve the response without a problem.

Thanks in advance.

Ross Presser
  • 6,027
  • 1
  • 34
  • 66
  • http://stackoverflow.com/questions/10860732/http-response-filter-cant-decode-the-response-bytes-the-second-time –  Jul 03 '13 at 03:49

2 Answers2

1

HTTP 403 error means "access forbidden". The destination website is refusing to fulfill your request, for reasons of its own.

Given this particular website http://iunlocker.net/, I'm going to hazard a guess that it may be checking the HTTP_REFERER. In other words it's refusing to fulfill your request because it knows it didn't come from a browser that was viewing the form.

[EDIT] After viewing the response from

curl --form ime_i=013270000134001 -i http://iunlocker.net/check_imei.php

I can see that the immediate response is setting a cookie and a redirect.

HTTP/1.1 307 Temporary Redirect
Server: nginx
Date: Wed, 03 Jul 2013 04:00:27 GMT
Content-Type: text/html
Content-Length: 180
Connection: keep-alive
Set-Cookie: PMBC=35e9e4cd3a7f9d50e7f3bb39d43750d1; path=/
Location: http://iunlocker.net/check_imei.php?pmtry=1

<html>
<head><title>307 Temporary Redirect</title></head>
<body bgcolor="white">
<center><h1>307 Temporary Redirect</h1></center>
<hr><center>nginx</center>
</body>
</html>

This site does not want you scraping it; if you wish to defeat this you will have to make use of its cookies.

Ross Presser
  • 6,027
  • 1
  • 34
  • 66
  • Thanks for your help,this website is only an example because i often found website with this type of pretection and i m really interested to know how this method works and how retrive data correctly,maybe for increase the security level in my website. – user2544906 Jul 03 '13 at 04:32
  • Thanks for your help,this website is only an example because i often found website with this type of pretection and i m really interested to know how this method works and how retrive data correctly,maybe for increase the security level in my website.So do i need strictly use cookies?I can learn myself how tu use this but can you maybe help me better?How can i found the value of this cooking without using a browser? – user2544906 Jul 03 '13 at 04:40
0

http://en.wikipedia.org/wiki/HTTP_403 - The web server is denying you access to that URL.

Perhaps the IP address you are using, is not allowed to access that resource. Check web server.

Shafiq Abbas
  • 484
  • 1
  • 5
  • 18
  • No problem with my Ip because making an httrequest with a browser i receive the response without problems.It s not the first i see this type of protection and i would understand better how it works – user2544906 Jul 03 '13 at 04:45