我想在craigslist.org上做flagging,很像是投票的系统,各位对这个不了解也没关系,因为我现在遇到的问题是HttpWebResponse导致的问题。开始是可以的,不过当我用了WebProxy来换IP时,出现了以下问题:
The remote server returned an error: (400) Bad Requestcode :HttpWebRequest myWebRequest = WebRequest.Create("
http://flag.craigslist.org/?flagCode=28&postingID=884589438") as
HttpWebRequest;WebProxy myProxy=new WebProxy();// Print the Proxy Url to the console.
Console.WriteLine("\nThe actual default Proxy settings are {0}",myProxy.
Address);
try
{
string proxyAddress;
proxyAddress = "http://143.215.129.117:3128";
if (proxyAddress.Length > 0)
{
// Create a new Uri object.
Uri newUri = new Uri(proxyAddress);
// Associate the newUri object to 'myProxy' object so that new
myProxy settings can be set.
myProxy.Address = newUri;
myWebRequest.Proxy = myProxy;//when i comment this, it works
} Console.WriteLine("\nThe Address of the new Proxy settings are {0}",
myProxy.Address);
HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse
();
Console.WriteLine(new StreamReader(myWebResponse.GetResponseStream()).
ReadToEnd());
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
是不是你的网络与Proxy之间的连接不好?
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(urlStr); //建立HttpWebRequest對象
hwr.Timeout = 60000; //定義服務器超時時間
WebProxy proxy = new WebProxy(); //定義一個網關對象
proxy.Address = new Uri("http://proxy.domain.com:3128"); //網關服務器:端口
proxy.Credentials = new NetworkCredential("f3210316", "6978233"); //用戶名,密碼
hwr.UseDefaultCredentials = true; //啟用網關認証
hwr.Proxy = proxy; //設置網關
HttpWebResponse hwrs = (HttpWebResponse)hwr.GetResponse(); //取得回應
Stream s = hwrs.GetResponseStream(); //得到回應的流對象
StreamReader sr = new StreamReader(s, Encoding.UTF8); //以UTF-8編碼讀取流
StringBuilder content = new StringBuilder(); //
while (sr.Peek() != -1) //每次讀取一行,直到
{ //下一個字節沒有內容
content.Append(sr.ReadLine()+"\r\n"); //返回為止
} //
return content.ToString() ; //返回得到的字符串
我想关键在于你作为Proxy的IP的问题,网上发布的IP在一段时间后可能就会被关闭掉,所以才出现时好时坏的情况!
建议你找几个稳定的IP或者代理软件试下!