How to send a HttpWebRequest with proxy address from Windows phone application - c#

I am developing a Windows phone mobile application. I need to send a WebRequest for a specific URL. See below
HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(#"http://www.test.html");
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
return true;//site available
else
return false;
}
Obviously, It works well for network without proxy. For network using proxy, I need to add the proxy information along with Webrequest. Since we are doing phone applcaition, We cant hardcode the proxy address in the Webrequest object.
How to send the Webrequest where the network is using proxy(from Windows phone application)

So far I couldn't find answer for it. However, I have handled the proxy in my desktop application. you need to add the following settings in you app.config file. This settings will add proxy information in all your WebRequest calls. Not sure whether this could help for your mobile app!
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<!--Provide your Proxy address-->
<system.net>
<defaultProxy>
<proxy
proxyaddress="[IP Address]:[port]"
/>
</defaultProxy>
</system.net>
</configuration>

Related

Can IIS be set up to block API Responses over a certain limit?

We have an application that acts as an API Gateway, taking calls and routing them through to other APIs and then relaying the responses.
In our set up, we make sure that developers making API calls don't try and flood us by uploading massive files or sending in excessive JSON payloads. We're now trying to figure out how we make sure that we don't flood them in return if they make an API call that returns too much data.
Is there a way to set up either IIS or an ASP.Net app to refuses API responses over a certain size in the same way that we would refuse an API Request over a certain size?
Just to add an example in clear terms:
If a developer makes an API request to get all of their customers, the API Gateway passes that request to our internal Customers API. If the Customers API responds with a 600MB response, we want to block the response at the API Gateway and then we'll send a response to the developer asking them to change their request to reduce the resultset.
The response has not limit the size. We can limit the response size by add more parameter Content-Length into the response header.
to set Request limit you could set in web.config file:
<system.web>
<httpRuntime maxRequestLength="2147483647" />
or
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483648" />
</requestFiltering>
</security>
</system.webServer>
Or
You could also set below code in web.config:
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
maxJsonLength Configures the maximum length of the JSON string.
For more detail, you could refer:
jsonSerialization Element (ASP.NET Settings Schema)
Regards,
jalpa

C# 407 Proxy Authentication Required

I want to make a request to a resource (that resource is behind a proxy). I have the proxy address and the port as well. I have tried with NetworkCredentialn no success, with CacheCredentials no success. WebException is:
ProtocolError
The remote server returned an error: (407) Proxy Authentification Required
I always get error at this line:
WebResponse response = request.GetResponse();
I have already done this:
Package manager in Visual Studio 2015 "407 (Proxy Authentication Required)"
I tried to configure my App.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true" />
</system.net>
</configuration>
From our corporate network, we usually employ this code:
WebProxy proxy = new WebProxy("http://your.proxy.server:8080", true);
proxy.Credentials = new NetworkCredential("user", "password");
WebRequest.DefaultWebProxy = proxy;
The idea is you put this code somewhere at the beginning of your program (or in the App start if you're on IIS) and then every single request will take the default proxy configuration.
No change in web.config is required. AFAICT, in web.config you cannot set the credentials.
In my experience, it works also for web services and WCF communications.

WebRequest.Proxy - What is the default value

Background: We have a service installed at a remote site that has started failing to call an external webservice. According to the network\support engineers on site no proxy should be required, but when attempting to execute the WebRequest an exception is returned:
System.Net.WebException: The remote server returned an error: (407) Proxy Authentication Required.
The exception is relatively self-explanatory, but looking at the code, no Proxy is specified in the call to the WebRequest and no proxy is defined in the app.config file.
Referring to the MSDN documentation for WebRequest.Proxy:
"The IWebProxy object to use to proxy the request. The default value is set by calling the GlobalProxySelection.Select property."
and then referring to the documentation for GlobalProxySelection.Select:
"Gets or sets the global HTTP proxy."
And given that GlobalProxySelection.Select is not set anywhere in the application, where does the default value come from? And are there any other global config files where the global HTTP proxy may be set?
You have to set the proxy manually. You can do it by doing the following, if you want to use the Default system proxy. (Internet settings in windows)
myRequest.Proxy = WebRequest.GetSystemWebProxy();
You are getting the error because your network is blocking access to the server directly without using a proxy.
While this doesn't solve your problem directly, can I recommend trying a <defaultProxy /> element in your app.config / web.config under <system.net> with useDefaultCredentials="true" - this may well get your application authenticating through the proxy and therefore not requiring a special bypass rule at all.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true" />
</system.net>
</configuration>

HTTP status 407: Proxy authentication required Error when calling web service

I have spent two hours on this error "HTTP status 407: Proxy authentication required Error " when calling a web service, my code is like following
WebProxy oWebProxy = new System.Net.WebProxy(ProxyServer, ProxyPort);
oWebProxy.Credentials = new NetworkCredential(ProxyUser,ProxyPassword,ProxyDomain);
oserv.Proxy = oWebProxy;
oserv.Credentials = new NetworkCredential(theusername, thepassword);
I have verified that the proxy address, user id password are all correct, and I could access the web service thru the IE in same pc, but when I run the code with VS, the error keeps popup.
I have tried the UserDefaultCredentials=true as well, but no luck.
Any idea?
Popping a<defaultProxy /> element into in app.config / web.config under <system.net> with useDefaultCredentials="true" may well do the job.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true" />
</system.net>
</configuration>
Try to run the application in on same network where the webservice is installed

getting "remote server cannot be resolved" on localhost does not get fixed by defaultProxy?

here is the sitution, i am testing on my localhost from my machine at home (no proxy server and windows default firewall) and retrieving api.flickr.com xml file, when I come to work (that uses an ISA server to connect) I get "remote server could not be resolved" so I added these lines to the web.config
<system.net>
<defaultProxy>
<proxy
usesystemdefault="False"
proxyaddress="http://localhost"
bypassonlocal="True"
/>
<bypasslist>
<add address="[a-z]+\.flickr\.com\.+" />
</bypasslist>
</defaultProxy>
</system.net>
that returns:System.Net.WebException: The remote server returned an error: (404) Not Found.
what went wrong? thanks
There are two possible scenarios here:
1: If you are building a client app (e.g. Console or WinForms) and want to access http://localhost using WebClient or HttpWebRequest without any intervening proxies, then bypassonlocal="True" should accomplish this. In other words, your app.config should look like this:
<defaultProxy>
<proxy
usesystemdefault="False"
bypassonlocal="True"
/>
</defaultProxy>
</system.net>
2: if, however, you're trying to get your ASP.NET app (running on http://localhost) to be able to correctly resolve URIs either with a proxy or without one, then you'll need to set up proxy info correctly in your web.config (or in machine.config so you won't have to change your app's web.config), so ASP.NET will know that you are running a proxy or not running one. Like this:
Home:
<defaultProxy>
<proxy
usesystemdefault="False"
bypassonlocal="True"
/>
</defaultProxy>
</system.net>
Work:
<defaultProxy>
<proxy
usesystemdefault="False"
proxyaddress="http://yourproxyserver:8080"
bypassonlocal="True"
/>
</defaultProxy>
</system.net>
It's also possible to use proxy auto-detection, to pick up settings from the registry, etc. but I've always shied away from those approaches for servers apps... too fragile.
BTW, if you find that things are configured correctly, and you still get the error, the first thing I'd recommend is to code up a quick test which manually sets the proxy before your WebClient/HttpWebRequest call, instead of relying on configuration to do it. Like this:
WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true);
WebClient wc = new WebClient();
wc.Proxy = proxyObject;
string s = wc.DownloadString ("http://www.google.com");
If the requests don't go through your work proxy correctly even when you're using code, even if the proxy is correctly configured in your code, then the proxy itself may be the problem.
In WebClient to download data from local no issues but downloading from internet is problem so configure the following
In your Web.config add below lines and replace your Intenet proxy Address and port
<system.net>
<defaultProxy useDefaultCredentials="true" enabled="true">
<proxy usesystemdefault="False" proxyaddress="http://your proxy address:port" bypassonlocal="True" />
</defaultProxy>
<settings>
<servicePointManager expect100Continue="false" />
</settings> </system.net>
now your program logic work for downloading content from internet and public URLS.

Categories