解码响应c#

本文关键字:响应 解码 | 更新日期: 2023-09-27 18:20:57

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

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


  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的相同消息,但我可以毫无问题地检索响应。

提前谢谢。

解码响应c#

HTTP 403错误表示"禁止访问"。目的地网站出于自身原因拒绝满足您的请求。

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

[EDIT]查看的响应后

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。

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

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