3

我正在使用以下代码向网站发出 HttpWebRequests:

public static HttpWebResponse SendGETRequest(string url, string agent, CookieContainer cookieContainer)
{
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
   request.UserAgent = agent;
   request.Method = "GET";
   request.ContentType = "text/html";
   request.CookieContainer = cookieContainer;

   return (HttpWebResponse)request.GetResponse();
}

在我尝试使用一个新网页并且只收到页面的最后一部分之前,一切都可以在几个网页上正常工作。这是收到的响应:

<tr> 
    <td colspan="2" height="5"><spacer type="block" width="100%" height="5"></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

标头是正确的,并表示只发送接收到的数据。以下是请求和响应的标头:

要求:

GET /Broker/Ops/FichaContratoJS.asp?nc=815044&IP=5&YY=2012&M=6&St=0&CC=FESX201206 HTTP/1.1  
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19  
Content-Type: text/html  
Host: www.xxxx.com  
Cookie: ASPSESSIONIDACBDCDBT=MGDNMNABOANDMILHBNCIDFCH;Autenticacion=Sid=230fae3d%2De0e2%2D4df1%2D8aa8%2D000fb352eaef&IdUsuarioWeb=xxxx; ASPSESSIONIDACBCCDAT=AFDJMNABAFJDDHABLOLAINDK; ASPSESSIONIDCADCBCAT=CEBJGNABLCALPJLDJFPBMLDE

回复:

HTTP/1.1 200 OK  
Date: Wed, 09 May 2012 07:25:03 GMT  
Server: Microsoft-IIS/6.0  
X-Powered-By: ASP.NET  
Pragma: no-cache  
**Content-Length: 155**  
Content-Type: text/html  
Expires: Wed, 09 May 2012 07:24:02 GMT  
Set-Cookie: Autenticacion=Sid=230fae3d%2De0e2%2D4df1%2D8aa8%2D000fb352eaef&IdUsuarioWeb=xxxx; path=/  
Cache-control: no-cache  

对 Web 浏览器执行相同操作可以正常工作,并返回大约 4000 字节的内容长度。

有任何想法吗?

PD:为了以防万一,我从不同线程到同一个站点对 SendGETRequest 进行了多次调用,但由于没有共享变量,我认为它不应该有所作为。

编辑:这是我用来从流中提取文本的扩展:

    public static string ReadTextResponse(this Stream stream)
    {
        int count;
        Encoding enconding = System.Text.Encoding.GetEncoding(1252);
        System.Text.StringBuilder stringBuilder = new StringBuilder();
        byte[] buffer = new byte[1023];

        do
        {
            count = stream.Read(buffer, 0, buffer.Length);

            if (count != 0)
            {
                string tempString = enconding.GetString(buffer, 0, count);
                stringBuilder.Append(tempString);
            }
        }
        while (count > 0);

        return stringBuilder.ToString();
    }

据我所知这是正确的。另外,请注意来自服务器的响应标头包含截断数据的长度

4

3 回答 3

1

我认为您没有使用正确的 HttpWebResponse 对象。

也许您没有关闭请求或阅读所有响应流。

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse.aspx

你的方法应该是:

public static string SendGETRequest(string url, string agent, CookieContainer cookieContainer)
    {
        var request = (HttpWebRequest)WebRequest.Create(url);
        request.UserAgent = agent;
        request.Method = "GET";
        request.ContentType = "text/html";
        request.CookieContainer = cookieContainer;

        string result;
        using (var myResponse = (HttpWebResponse) request.GetResponse())
        {
            using (var stream = myResponse.GetResponseStream())
            {
                result = null;
                if (stream != null)
                {
                    using (var sr = new StreamReader(stream, System.Text.Encoding.UTF8))
                    {
                        result = sr.ReadToEnd();
                        sr.Close();
                    }
                    stream.Close();
                }
            }
            myResponse.Close();
        }
        return result;
    }
于 2012-05-09T08:14:03.070 回答
0

难以置信...我发送的 URL /Broker/Ops/FichaContratoJS.asp?nc=815044&IP=5&YY=2012&M=6 而浏览器发送的是 /Broker/Ops/FichaContratoJS.asp?nc=815044&IP=5&YY=2012&M=06& (注意 M 参数上的额外 0 (这是一个月)。放在那里 0 会返回整页。听起来对我来说是个缺陷

于 2012-05-25T10:52:53.833 回答
0

我遇到了类似的情况,发现将响应流复制到 MemoryStream 似乎可以解决我的问题。

public static string SendGETRequest(string url, string agent, CookieContainer cookieContainer)
{
    var request = (HttpWebRequest)WebRequest.Create(url);
    request.UserAgent = agent;
    request.Method = "GET";
    request.ContentType = "text/html";
    request.CookieContainer = cookieContainer;

    string result;
    using (var myResponse = (HttpWebResponse) request.GetResponse())
    {
        using (var stream = myResponse.GetResponseStream())
        {
            result = null;
            if (stream != null)
            {
                MemoryStream memStream = new MemoryStream();
                stream.CopyTo(memStream);
                memStream.Flush();
                stream.Close();

                using (var sr = new StreamReader(memStream, System.Text.Encoding.UTF8))
                {
                    result = sr.ReadToEnd();
                    sr.Close();
                }

            memStream.Close();
            }
        }
        myResponse.Close();
    }
    return result;
}
于 2014-03-12T14:27:32.407 回答