我正在开发一个需要下载某个网页内容的项目,我在C#中的代码运行得很完美,但是,似乎Python无法连接到互联网抛出异常。
也许是因为我在企业网络中工作,这就是为什么Python可能被防火墙阻止的原因。但是,C#代码怎么样,为什么会有效?
我想将Python代码模仿为C#WebClient行为。
感谢。
C#代码
WebClient wc = new WebClient();
string str = wc.DownloadString("https://www.google.com");
Console.WriteLine(str);
Pythton Code
import urllib
link = "https://www.google.com"
f = urllib.urlopen(link)
myfile = f.read()
print myfile
Python异常
[Errno套接字错误] [Errno 10060]连接尝试失败,因为 一段时间后,关联方没有正确回复, 或已建立的连接失败,因为连接的主机失败 响应
尝试WebClient.Proxy = null
WebClient wc = new WebClient();
wc.Proxy = null;
string str = wc.DownloadString("https://www.google.com");
Console.WriteLine(str);
然后我得到了这个例外。 (I also got this exception in a Java Program
)
{"操作已超时"}
at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
at System.Net.WebClient.DownloadString(Uri address)
at System.Net.WebClient.DownloadString(String address)
at WebClientTest.Program.Main(String[] args) in <ProjectPath>\WebClientTest\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()