C# webrequest 或 webclient 问题

发布于 2024-11-07 10:29:52 字数 2081 浏览 0 评论 0原文

我正在我的应用程序中实现一个论坛板。 该页面通过 webrequest 对象下载,然后将其应用于 Web 浏览器控件的文档文本(必须通过 webrequest 访问该页面),并且缺少元素和正常广告。我该如何解决这个问题?

这是我正在使用的函数:

        private void Navigate(string url, string credentials, ref WebBrowser wbBrowser)
        {
            WebRequest req;

            WebProxy proxy;

            WebResponse response;

            StreamReader sr;

            int index;

            string  username,
                    password,
                    ipAddress,
                    temp;


            index = 0;

            try
            {
                for (int i = 0; i < 2; i++)
                    index = credentials.IndexOf(':', index + 1);

                if (index != -1)
                {
                    ipAddress = credentials.Remove(index, credentials.Length - index);

                    temp = credentials.Remove(0, index + 1);

                    index = temp.IndexOf(':');

                    username = temp.Remove(index, temp.Length - index);
                    password = temp.Remove(0, index + 1);

                    proxy = new WebProxy(ipAddress, true);
                    proxy.Credentials = new NetworkCredential(username, password);

                    req = WebRequest.Create(url);
                    req.Proxy = proxy;

                    response = req.GetResponse();


                    sr = new StreamReader(response.GetResponseStream());

                    temp = sr.ReadToEnd();

                    index = temp.IndexOf("<head>");

                    if (index != -1)
                        temp = temp.Insert(index + 6, "<base href=\"" + url + " \" />");

                    mainDocument = null;

                    wbBrowser.DocumentText = temp;

                    while (mainDocument == null)
                        Thread.Sleep(250);
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.ToString());
            }
        }

我将其用作 Navigate("page", "ip:port:username:password", ref demoBrowser)

I am implementing a forum board into my application.
The page is downloaded with a webrequest object, then applied it to the document text of a web browser control (must access the page through webrequest) and there is elements and normal advertisements missing. how can I fix this?

This is the function I am using:

        private void Navigate(string url, string credentials, ref WebBrowser wbBrowser)
        {
            WebRequest req;

            WebProxy proxy;

            WebResponse response;

            StreamReader sr;

            int index;

            string  username,
                    password,
                    ipAddress,
                    temp;


            index = 0;

            try
            {
                for (int i = 0; i < 2; i++)
                    index = credentials.IndexOf(':', index + 1);

                if (index != -1)
                {
                    ipAddress = credentials.Remove(index, credentials.Length - index);

                    temp = credentials.Remove(0, index + 1);

                    index = temp.IndexOf(':');

                    username = temp.Remove(index, temp.Length - index);
                    password = temp.Remove(0, index + 1);

                    proxy = new WebProxy(ipAddress, true);
                    proxy.Credentials = new NetworkCredential(username, password);

                    req = WebRequest.Create(url);
                    req.Proxy = proxy;

                    response = req.GetResponse();


                    sr = new StreamReader(response.GetResponseStream());

                    temp = sr.ReadToEnd();

                    index = temp.IndexOf("<head>");

                    if (index != -1)
                        temp = temp.Insert(index + 6, "<base href=\"" + url + " \" />");

                    mainDocument = null;

                    wbBrowser.DocumentText = temp;

                    while (mainDocument == null)
                        Thread.Sleep(250);
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.ToString());
            }
        }

I use it as Navigate("page", "ip:port:username:password", ref demoBrowser)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2024-11-14 10:29:52

我将引用您的其他问题中的内容,因为当不处理代理:

这里的问题是,您通过 WebRequest 收到的 HTML 包含当前上下文中不存在的 CSS 文件的相对路径。您可以通过在该部分中添加以下标记来修改 HTML:

<base href="http://domainname.com/" />

之后,WebBrowser 控件将解析此标记中域的相对 CSS 路径。

I'll quote myself from your other question, because it makes perfect sense when not dealing with proxies:

Well the problem here is that the HTML that you've received via the WebRequest contains relative paths to the CSS files that are not present in the current context. You can modify the HTML, by adding the following tag in the section:

<base href="http://domainname.com/" />

After that the WebBrowser control resolves the relative CSS paths to the domain in this tag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文