Is it possible to add to the entire application written in C # used a proxy. Mainly because of the class I pasted below. But I also use several times the usual WebRequest.
I would like the program to use the proxy for each connection that will perform. Is it possible?
using System;
using System.Diagnostics;
using System.Net;
namespace TestV.1.0
{
// To jest stworzenie klasy HttpHandler, dzieki której możemy pracować na http. Dziedziczy ona z klasy WebClient
internal class HttpHandler : WebClient
{
//Tworzenie obiektu który będzie przechochowywał cookiesy
public CookieContainer _container = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = _container;
}
return request;
}
protected override WebResponse GetWebResponse(WebRequest request)
{
var response = base.GetWebResponse(request);
if (response != null)
{
var uriBuilder = new UriBuilder(response.ResponseUri.Scheme, response.ResponseUri.Host);
_container.SetCookies(uriBuilder.Uri, _container.GetCookieHeader(request.RequestUri));
}
return response;
}
public void ClearCookies()
{
_container = new CookieContainer();
}
}
}
Aucun commentaire:
Enregistrer un commentaire