如何在 C# 中使用具有代理支持的 http post
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1372519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use http post with proxy support in c#
提问by monkey_boys
How to use http post with proxy support in c# and multipart form data upload method
如何在c#中使用带有代理支持的http post和多部分表单数据上传方法
采纳答案by Druid
This post by Brian Grinsteadexplains how you can do just that.
Brian Grinstead 的这篇文章解释了如何做到这一点。
For proxy support, you only need to pass a Proxy
setting to HttpWebRequest
. So, in the above example, you would change:
对于代理支持,您只需将Proxy
设置传递给HttpWebRequest
. 因此,在上面的示例中,您将更改:
HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
To:
到:
string MyProxyHostString = "192.168.1.200";
int MyProxyPort = 8080;
HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
request.Proxy = new WebProxy (MyProxyHostString, MyProxyPort);
回答by AnthonyWJones
If you need to configue a proxy then you can do so in the .config file:-
如果您需要配置代理,那么您可以在 .config 文件中进行:-
<system.net>
<defaultProxy enabled="true">
<proxy proxyaddress="http://myproxyserver:8080" bypassonlocal="True"/>
</defaultProxy>
</system.net>
See this questionon form data posting.
请参阅有关表单数据发布的此问题。
回答by Nagaraj Raveendran
If the web request works fine in your localhost with default proxy and not working in your web server, then you have to set your company's approved proxy and also whitelist the URL you are connecting to from your web application in the web server.
如果 Web 请求在您的本地主机中使用默认代理正常运行,但在您的 Web 服务器中不起作用,那么您必须设置公司批准的代理,并将您从 Web 服务器中的 Web 应用程序连接到的 URL 列入白名单。
You can mention the proxy settings either in web.config or in code.
您可以在 web.config 或代码中提及代理设置。
<system.net>
<defaultProxy enabled="true">
<proxy proxyaddress="http://yourcompanyproxyserver:8080" bypassonlocal="True"/>
</defaultProxy>
</system.net>
(or)
(或者)
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("URL");
wr.Proxy = new WebProxy("companyProxy",Portnumber);
wr.Method = "POST";