Programmatically changing defaultproxy instead of using app.config - c#

I'm creating a desktop application in WPF. This application uses webclient instances to communicate with an API to collect data from.
In this desktop application I want to create a checkbox which should allow the user to ignore the internet options proxy or to use the default auto detect options.
At this moment I added this defaultproxy setting to my app.config to stop my application from trying to communicate through the proxy and instead ignore it.
<system.net>
<defaultProxy enabled="false" useDefaultCredentials="false">
<proxy/>
<bypasslist/>
<module/>
</defaultProxy>
</system.net>
When I don't have this code in my app.config, my application will try to use the default internet options proxy.
So in order to have this switchable by a checkbox in a settings menu I will have to change these settings programatically.
I am aware of the fact that when I set the default proxy to a new webproxy that the application will ignore the internetoptions proxy.
WebRequest.DefaultWebProxy = new WebProxy();
But I can't for the life of me figure out how I can set this back to the automatically detecting of proxy use, like before I inserted above defaultproxy settings in the app.config.
I am testing this by using a faulty proxy. This means that if I send an api request, the proxy can't be found and I receive a webexception. When I ignore the proxy with the app.config code, the request uses my normal internet connection, and returns API data.
It would really help me out if anyone could tell me how I can programatically set my application to ignore the proxy or, most importantly, to use the default auto detection settings.

If you want to set the proxy back to the default proxy, you can use this static method
WebRequest.GetSystemWebProxy();
see here https://msdn.microsoft.com/de-de/library/system.net.webrequest.getsystemwebproxy(v=vs.110).aspx

Related

How could I use a Proxy with RedditSharp?

I just started to play around with RedditSharp (https://github.com/ddevault/RedditSharp) and I know I got a bit late to the party and I should be using OAuth instead, but I found it curious that this thing is still working by simply interacting with old.reddit.com.
My question is: is there any way I could connect to reddit using this library and a proxy?
I found this section (How to use a proxy with RedditSharp?), tried it but with no luck and couldn't find anything else besides that.
Thank you so much for the help.
I've tried the only solution found here: How to use a proxy with RedditSharp? but it seems is ignoring that I am using a proxy.
RedditSharp is a bit old ( 3 years old and un-maintained ). It seems RedditSharp is using WebRequest to make HTTP requests. In this case, you can use DefaultWebProxy property of WebRequest class.
WebRequest.DefaultWebProxy
The DefaultWebProxy property gets or sets the global proxy. The
DefaultWebProxy property determines the default proxy that all
WebRequest instances use if the request supports proxies and no proxy
is set explicitly using the Proxy property. Proxies are currently
supported by FtpWebRequest and HttpWebRequest.
The DefaultWebProxy property reads proxy settings from the app.config
file. If there is no config file, the current user's Internet Explorer
(IE) proxy settings are used.
If the DefaultWebProxy property is set to null, all subsequent
instances of the WebRequest class created by the Create or
CreateDefault methods do not have a proxy.
Example:
var proxyObject = new WebProxy("http://proxyserver:80/",true);
WebRequest.DefaultWebProxy = proxyObject;
// init RedditSharp stuff here
You can read more about WebProxy here.

WebService call works from WinForms app but not from WebApp

I am trying to call a webservice to get information about specific objects. It works perfectly fine in my test WinForms application, basically after I added the service reference I did not have to do anything else than creating a local client in my C# class, setting up the input parameters and calling the method that was in the documentation and I received the response I wanted. Very simple webservive, no https, no magic.
I copied my code to my MVC web application, I made sure to copy EXACTLY the same url, web.config settings (bindings, endpoints), I even updated the service reference many times, but in MVC it does not work. The error message says:
"There was no endpoint listening at 'webservicename' that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details."
The inner exception is:
"No connection could be made because the target machine actively refused it 127.0.0.1:8888"
I have set up my hosts file, because I am using Local IIS and wanted to use the websie's name in the url, so I have this in my hosts:
"127.0.0.1 www.mywebsite.local"
I have a feeling that either my firewall or app pool setting causes this problem, but I could not figure out which one. Once again, the webservice is working perfectly fine from my basic test WinForms app.
I tried to change the Identity of my app pool to something else than ApplicationPoolIdentity (I tried NetworkService, LocalService, LocalSystem), but it did not help.
.Net version is 4.5 in WinForms and 4.5.1 in MVC.
Any ideas?
Edit:
It looks like there is a problem with the proxy settings. In the MVC web.config I had the following section:
<system.net>
<defaultProxy enabled="true">
<proxy proxyaddress="http://127.0.0.1:8888" />
</defaultProxy>
I do not know why, but when I set it to false, the problem goes away...I am not an expert of proxies. Does it mean I have to add special settings to my firewall?

How to set a proxy auto-configuration script (.pac) to be used in C#?

I'm starting to write some very simple web-apps in C# and occasionally I get exceptions about not having a proxy configured. I am in a work environment that has a rather strict proxy auto-configuration file (.pac -- Proxy Auto-Config).
Is there a way to tell C# to use that .pac file for proxy settings?
.NET can use the default proxy settings of the user running the code. It uses the same settings that are defined for IE. See this article: https://msdn.microsoft.com/en-us/library/dkwyc043%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
You should also see this SO post about MIME types for the PAC file: Using Proxy Automatic Configuration from IE Settings in .Net

How to disable Expect:100-Continue when using WCF in Windows Store App

I need to get rid of the Expect: 100-Continue header in HTTP message, when communicating with WebService using WCF in Windows Store App.
I have found a lot of solutions, but none of them is possible in Windows 8:
ServicePoint.Expect100Continue = false; doesn't exist in Store App any more (No SericePoint or ServicePointManager class),
I cannot change configuration in web.config file, because it doesn't exist in Store App,
It is possible to change the flag in HttpClient, but I cannot access it, when using WCF,
I tried to manipulate with message headers using IClientMessageInspector, but the default HTTP headers are being added later, in higher layers, so my changes will be ovverriden.
Does anyone have any other ideas?
There are 2 ways to deal with this.
You can turn set expect100Continue="false" in you app.config or web.config, but this will be global. This could be an issue if some external services prefer to use the header expect100Continue. Here's how:
<system.net>
<settings>
<servicePointManager expect100Continue="false"/>
</settings>
</system.net>
Or you can do it in code for a specific endpoint as follows:
System.Net.ServicePoint servicePoint =
System.Net.ServicePointManager.FindServicePoint(myWcfService.Endpoint.Address.Uri);
servicePoint.Expect100Continue = false;
// now execute some service operation

MetadataExchangeClient using Web Proxy

I am trying to connect to a URI pointing to a WSDL file which describes a WCF service by using the MetadataExchangeClient class.
var mexClient = new MetadataExchangeClient(uri, MetadataExchangeClientMode.HttpGet);
var metaDocs = mexClient.GetMetadata();
The problem is that I cannot access this uri directly and need to go through a HTTP proxy server.
The proxy server URL can change at runtime so I cannot simply configure it through the app.config file.
Where can I specify the proxy server information in the MetadataExchangeClient class?
Anyone got an idea? WCF experts?
You need to specify the proxy in the app.config like this:
<system.net>
<defaultProxy enabled="true">
<proxy bypassonlocal="true"
proxyaddress="http://proxy.domain.org:8888/" />
</defaultProxy>
</system.net>
You can override the GetChannelFactory and provide an implementation that can create a suitable endpoint i.e. a BasicHtppBinding with the proxy details added.
See here for a clue to how programmatically set Binding.
How can I set an HTTP Proxy (WebProxy) on a WCF client-side Service proxy?
See here for starting point on creating the Channel Factory (see the 3 comments at the end of the post)
http://msdn.itags.org/visual-studio/83755/
This shows how to use MetadataExchangeClient with a Custom Binding:
http://wikiencyclopedia.net/WCF/3.5.30729.1/untmp/Orcas/SP/ndp/cdf/src/WCF/infocard/Service/managed/Microsoft/InfoCards/InfoCardMetadataExchangeClient.cs/2/InfoCardMetadataExchangeClient.cs
Here's the .NET Framework Source to MetadataExchangeClient so you can get a better understanding of what it is doing.
http://reflector.webtropy.com/default.aspx/WCF/WCF/3#5#30729#1/untmp/Orcas/SP/ndp/cdf/src/WCF/ServiceModel/System/ServiceModel/Description/MetadataExchangeClient#cs/1/MetadataExchangeClient#cs

Categories

Resources