C# webrequest 或 webclient 问题
我正在我的应用程序中实现一个论坛板。 该页面通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将引用您的其他问题中的内容,因为当不处理代理:
这里的问题是,您通过 WebRequest 收到的 HTML 包含当前上下文中不存在的 CSS 文件的相对路径。您可以通过在该部分中添加以下标记来修改 HTML:
之后,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:
After that the WebBrowser control resolves the relative CSS paths to the domain in this tag.