webbrowser对象的C#代理

时间:2013-12-06 21:47:18

标签: c# .net

我想用C#创建一个程序,使用代理服务器进入网站。 如果我运行这个程序,当我调试它时,我在第37行遇到了错误。你能帮我吗? 我总是得到时间错误的错误。我改变了TimeOut,但是 它永远不会做任何事情。

txtUrl是一个文本框,您需要在其中放入您想访问的网站的网址。 txtProxyPath是一个用作openfiledialog的文本框, 在那里我可以选择一个包含代理ips的.txt文件。

这里是我使用的代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace YoutubeRefresh
{
    public partial class Form1 : Form
    {
        static private ArrayList listProxy = new ArrayList();
        static private short proxyShort = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            Uri uriAdress = new Uri(txtUrl.Text);
            object[] arrayProxy = listProxy.ToArray();
            WebProxy proxyObject = new WebProxy(arrayProxy[proxyShort].ToString());
            MessageBox.Show("Proxyserver used: " + arrayProxy[proxyShort].ToString());
            proxyShort += 1;
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(txtUrl.Text);
            webRequest.Proxy = proxyObject;


            // httpwepresponse error
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            Stream receiveStream = response.GetResponseStream();
            webBrowser1.DocumentStream = receiveStream; 
        }

        private void btnChoosListProxy_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                txtProxyPath.Text = openFileDialog1.FileName;
            }

            foreach (var v in File.ReadAllLines(txtProxyPath.Text))
            {
                listProxy.Add(v.ToString());
            }
        }
    }
}

非常感谢!

1 个答案:

答案 0 :(得分:0)

var request = (HttpWebRequest)WebRequest.Create("http://google.com");
request.Proxy = new WebProxy("IP", port);
var response = (HttpWebResponse)request.GetResponse();

using (var stream = response.GetResponseStream())
{
    if (stream != null)
    {
        using (var streamReader = new StreamReader(stream))
        {
            Console.WriteLine(streamReader.ReadToEnd());
        }
    }
}

同样在你的App.Config文件中,(如果你没有,创建一个)添加以下内容。

<configuration>
  <system.net>
    <settings>
      <servicePointManager expect100Continue="false"/>
    </settings>
  </system.net>
</configuration>