The proxy typically has a different port than the service you are trying to consume.
Here are some improvements you should consider:
* SimpleHttp is no real "class" in object-orientated manner. You have simply a class that contains a method. Better: Provide an IStringDataProvider Interface which could look like:
public interface IStringDataProvider
{
string GetContent();
}
Then you are able to get the content from other sources than http - and your testing will be easier as you can replace it with a mock-provider. The url can be passed to the http-provider-instance with dependency injection (see code below).
* GetUrl sounds like "Hey, get me the url!". Better: GetContent or GetContentByUrl
* Dont pass all the proxy-propertys on each request. Better: Set the
WebRequest.DefaultWebProxy
at application startup. Then you are independent of using a proxy or not and your whole application will use the same proxy.
* Dont swallow exceptions! Either you have a TryGetContent-Method returning whether the operation succeeded or you handle the exception when you want to check whether an error occured - this is typically done in the GUI.
Putting all together:
interface IStringDataProvider
{
string GetContent();
}
class HttpStringDataProvider : IStringDataProvider
{
public HttpStringDataProvider(string url)
{
Url = url;
}
public string Url { get; private set; }
private CookieContainer cookies = new CookieContainer();
public string GetContent()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.UserAgent = "Mozilla/5.0";
request.AllowAutoRedirect = true;
request.ReadWriteTimeout = 5000;
request.CookieContainer = cookies;
request.Referer = "";
request.Headers.Set("Accept-Language", "en,en-us");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
return streamReader.ReadToEnd();
}
}
On startup:
if (proxyType == "http")
{
WebProxy proxy = new WebProxy(myProxyHost, myProxyPort);
proxy.Credentials = new NetworkCredential(myProxyUsername, myProxyPassword);
WebRequest.DefaultWebProxy = proxy;
}
else if (proxyType == "system")
{
WebRequest.DefaultWebProxy = WebRequest.GetSystemWebProxy();
}
else if (proxyType == "no")
{
WebRequest.DefaultWebProxy = null;
}