Tuesday 18 May 2010

The remote server returned an error: (502) Bad Gateway.

The remote server returned an error: (502) Bad Gateway.

Simply I want to access internet-world from inside my web-application using the following code.

public static string BrowseURL(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "GET";
request.Credentials = CredentialCache.DefaultCredentials;
//
request.Proxy = WebProxy.GetDefaultProxy();
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
//
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(response.CharacterSet));
string responseData = reader.ReadToEnd();
return responseData;
}

On my development machine it works fine, but on production server I faced a problem of "Bad Gateway" when reaching GetResponse(), I realized that it was an authentication problem, and after some google_ing, I found a good thread that speaks about solving Proxy problems with authentication. Now, the following code is working properly on my production environment.

public static string BrowseURL(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "GET";
request.Credentials = CredentialCache.DefaultCredentials;
//that is the solution
WebProxy proxy = new WebProxy("proxy ");
proxy.Credentials = new NetworkCredential("userName", "Password", "Domain");
request.Proxy = proxy;
//
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(response.CharacterSet));
string responseData = reader.ReadToEnd();
return responseData;
}

Saturday 15 May 2010

Conditional buidl Events using Viusal Studio 2008

Sometimes you need some extra actions to be trigged after build, eg. copy exe files. To add an extra action, go to project properties and select “Build Events” tab. You can add the additional actions before and after build process.

But what if some conditions are needed to check before performing your action. !

Two things you have to investe in order to know how to make conditional build event in VS2008, first, is the command macros introduced by Microsoft in VS2008, second, is the batch file ".BAT" commands.


Command Macros are
here.
BAT Commands are
here.

Example:

IF NOT $(ConfigurationName) == Release GOTO end
Copy "$(SolutionDir)\$(ConfigurationName).exe" "$(TargetPath)"
:end

Wednesday 5 May 2010

C++ Macro Concatination

You can use ## operator (double number sign) to concatinate two tokens (text or arguments) in macro invocation.

Example:

enum Types {T1,T2};
#define MakeDrivedClass(name, type) class name##type : public type##Base{}
MakeDrivedClass(CMy,t1);

Output:
class CMyT1 :  public T1Base {};