0

我正在开发一些用于测试的 API,当我发出网络请求时,尤其是当我检索网络响应时,我遇到了问题。我使用这段代码:

字符串请求 = 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;    
        }

通话中的网站就是一个例子,因为有了这个网站和其他网站,我无法正确检索结果。我收到异常“错误 403”

谁能告诉我我可能做错了什么来帮助我?

我认为问题出在编码/解码上——实际上使用 Fiddler 它会在查看文本之前询问我是否要解码——但是对于另一个网站,例如,我从 Fiddler 收到相同的消息,但我可以检索响应没有问题。

提前致谢。

4

2 回答 2

1

HTTP 403错误意味着“禁止访问”。目标网站出于自身原因拒绝满足您的要求。

鉴于这个特定的网站http://iunlocker.net/,我会冒险猜测它可能正在检查 HTTP_REFERER。换句话说,它拒绝满足您的请求,因为它知道它不是来自正在查看表单的浏览器。

[编辑]查看来自的回复后

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

我可以看到即时响应是设置 cookie 和重定向。

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>

这个网站不想让你刮掉它;如果您想解决这个问题,您将不得不使用它的 cookie。

于 2013-07-03T03:58:39.183 回答
0

http://en.wikipedia.org/wiki/HTTP_403 - Web 服务器拒绝您访问该 URL。

也许您正在使用的 IP 地址不允许访问该资源。检查网络服务器。

于 2013-07-03T04:40:02.913 回答