如何使用InternetOpen,InternetOpenUrl和InternetReadFile [英] How To Use InternetOpen, InternetOpenUrl and InternetReadFile

查看:318
本文介绍了如何使用InternetOpen,InternetOpenUrl和InternetReadFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用winInet.dll下载WebPage内容

How I Download The WebPage Content Using winInet.dll

推荐答案

在c#中你不需要直接使用winInet.dll,你有几个.NET类可以使用:

WebClient [ ^ ]是wininet和wininet的包装器可能最容易使用。



WebRequest [ ^ ]它的后代将让你更好地控制创建请求和处理响应。



在你的情况下问题似乎是你需要处理cookies所以你连接的服务器可以使用session。

WebClient类不会处理默认情况下是e cookies,但你可以扩展它:

In c# you do not need to use winInet.dll directly, there are several .NET classes you can use:
WebClient[^] is a wrapper around wininet and probably easiest to use.

WebRequest[^] and it''s descendents will give you more control over creating requests and processing response.

In your case problem seems to be that you need to handle cookies so the server you''re connecting to can use session.
WebClient class does not handle cookies by default but you can extend it:
public class WebClientEx : WebClient
{
    private readonly CookieContainer _Container = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        HttpWebRequest webRequest = request as HttpWebRequest;
        if (webRequest != null)
        {
            webRequest.CookieContainer = _Container;
        }
        return request;
    }
}



并使用WebClientEx而不是WebClient。


and use WebClientEx instead of WebClient.


这篇关于如何使用InternetOpen,InternetOpenUrl和InternetReadFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆