How to use http post with proxy support in c# and multipart form data upload method
Asked
Active
Viewed 5.5k times
3 Answers
29
This post by Brian Grinstead explains how you can do just that.
For proxy support, you only need to pass a Proxy
setting to HttpWebRequest
. So, in the above example, you would change:
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);

Druid
- 6,423
- 4
- 41
- 56
-
The problem with this approach is it hard codes the proxy address/port into the compiled code. – AnthonyWJones Sep 08 '09 at 07:22
-
10That can easily be placed anywhere else. I put it in this way to explain the example better. – Druid Sep 11 '09 at 10:09
2
If you need to configue a proxy then you can do so in the .config file:-
<system.net>
<defaultProxy enabled="true">
<proxy proxyaddress="http://myproxyserver:8080" bypassonlocal="True"/>
</defaultProxy>
</system.net>
See this question on form data posting.

Community
- 1
- 1

AnthonyWJones
- 187,081
- 35
- 232
- 306
-
-
Sorry thought you we just asking about proxy support, however the largest part of the question is regarding multipart form data. – AnthonyWJones Sep 03 '09 at 10:39
-
can i use proxy in some one ex. http://www.proxy4free.com/page1.html to http post 189.80.133.186 8080 ?? – monkey_boys Sep 03 '09 at 10:39
-
Don't see why not, assuming you really want to expose your data to any Tom, Dick or Harry that might be running a free proxy server. – AnthonyWJones Sep 03 '09 at 10:44
-
"Proxy support ?" What exactly are you asking? The answer describes how you specify a proxy server that classes in System.Net namespace will use. – AnthonyWJones Sep 03 '09 at 12:31
1
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.
You can mention the proxy settings either in web.config or in code.
<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";

Jeff Puckett
- 37,464
- 17
- 118
- 167

Nagaraj Raveendran
- 1,150
- 15
- 23