拜托C++、C#高手,把下边的代码翻译成C#的

LPWSTRproxyName;
inton=1;
if(on){
proxyName=L"127.0.0.1:3125";
}
else{
proxyName=0;
}
INTERNET_PER_CONN_OPTION_LISTOptionList;
INTERNET_PER_CONN_OPTIONOption[3];
unsignedlonglistSize=sizeof(INTERNET_PER_CONN_OPTION_LIST);
Option[0].dwOption=INTERNET_PER_CONN_PROXY_SERVER;
Option[1].dwOption=INTERNET_PER_CONN_FLAGS;
Option[2].dwOption=INTERNET_PER_CONN_PROXY_BYPASS;
OptionList.dwSize=sizeof(INTERNET_PER_CONN_OPTION_LIST);
OptionList.pszConnection=NULL;
OptionList.dwOptionCount=3;
OptionList.dwOptionError=0;
DWORDproxyType=PROXY_TYPE_DIRECT;//thisproxytypedisablesanyproxyserver
if(proxyName){
if(proxyName[0]){
proxyType=PROXY_TYPE_PROXY;//anamehasbeenpassed,sochoosethecorrectproxytypeforenablingtheproxyserver
}
}
Option[0].Value.pszValue=(LPWSTR)proxyName;
Option[1].Value.dwValue=proxyType;
if(on){
Option[2].Value.pszValue=(LPWSTR)L"";
}
else{
Option[2].Value.pszValue=(LPWSTR)L"";
}
OptionList.pOptions=Option;
if(!InternetSetOption(0,INTERNET_OPTION_PER_CONNECTION_OPTION,&OptionList,listSize)){
//handleerror
}
InternetSetOption(0,INTERNET_OPTION_REFRESH,NULL,NULL);
=====================================================
就是上边这些代码,如果写成用C#调用WinInet的,应该怎么写?
炎炎设计
浏览 346回答 2
2回答

森林海

代码就是设置Proxy地址,在C#里面可以直接使用WebRequest和WebProxy来建立连接和设置代理。///Reference:///http://msdn.microsoft.com/en-us/library/system.net.webrequest.proxy(v=vs.110).aspx//////Usage:///createRequestWithProxy("http://www.segmentfault.com","127.0.0.1:3125")WebRequestcreateRequestWithProxy(string,url,stringproxyAddress){WebRequestwebRequest=WebRequest.Create(url);WebProxywebProxy=(WebProxy)webRequest.Proxy;try{webProxy.Address=newUri(proxyAddress);}catch(UriFormatExceptione){//donothing}returnwebRequest;}

芜湖不芜

WebRequestreq=WebRequest.Create(url);req.Proxy=newWebProxy("127.0.0.1:3125");当然如果是http的话,你也可以用WebClientWebClientwc=newWebClient();wc.Proxy=newWebProxy("127.0.0.1:3125");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript