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;//thisproxytypedisablesanyproxyserverif(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的,应该怎么写?
2 回答
森林海
TA贡献2011条经验 获得超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;}
芜湖不芜
TA贡献1796条经验 获得超7个赞
WebRequestreq=WebRequest.Create(url);req.Proxy=newWebProxy("127.0.0.1:3125");当然如果是http的话,你也可以用WebClientWebClientwc=newWebClient();wc.Proxy=newWebProxy("127.0.0.1:3125");
添加回答
举报
0/150
提交
取消