我使用FtpWebRequest做一些FTP的东西,我需要直接连接(没有代理)。但是WebRequest.DefaultWebProxy包含IE代理设置(我估计)。
WebRequest request = WebRequest.Create("ftp://someftpserver/");
// request.Proxy is null here so setting it to null does not have any effect
WebResponse response = request.GetResponse();
// connects using WebRequest.DefaultWebProxy
我的代码是一个巨大的应用程序中的一个部分,我不想更改WebRequest.DefaultWebProxy
,因为它是全局静态属性,它可能会对应用程序的其他部分产生负面影响。
知道该怎么做吗?
答案 0 :(得分:23)
尝试将代理设置为空的WebProxy,即:
request.Proxy = new WebProxy();
这应该创建一个空代理。
答案 1 :(得分:9)
实际上将其设置为null也足以禁用自动代理检测,您可以节省一些周期:)
request.Proxy = null;
答案 2 :(得分:0)
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(yourRequestUrl);
if (webRequest.Proxy != null)
{
webRequest.Proxy = null;
}
webRequest.KeepAlive = true;
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
var json = JsonConvert.SerializeObject(yourObject);
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] postBytes = encoder.GetBytes(json);
webRequest.ContentLength = postBytes.Length;
webRequest.CookieContainer = new CookieContainer();
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(string.Format("{0}:{1}", userName, password)));
webRequest.Headers.Add("Authorization", "Basic " + encoded);
Stream requestStream = webRequest.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
string result;
using (StreamReader rdr = new StreamReader(response.GetResponseStream()))
{
result = rdr.ReadToEnd();
}
答案 3 :(得分:0)
将此添加到配置中:
<system.net>
<defaultProxy enabled="false" useDefaultCredentials="false">
<proxy />
<bypasslist />
<module />
</defaultProxy>
</system.net>