Hi !!
I am trying to route all requests to my server to another server using HttpWebRequest and HttpWebResponse classes and HttpPHandlers. When i am requesting a page using this code i am able to sucessfully redirect the html, and css requests.. Sowever it fails for some requests to images.
i am pasting the code ..
public
class CommunicationHandler : IHttpHandler{
public CommunicationHandler(){
// // TODO: Add constructor logic here //}
#region
Implementation of IHttpHandler public void ProcessRequest(System.Web.HttpContext context){
HttpContext.Current.Response.Clear(); HttpWebRequest myReq; // HttpContext.Current.Request.Path if (!HttpContext.Current.Request.Url.PathAndQuery.Contains("ClientMIS")){
myReq = (
HttpWebRequest)WebRequest.Create("http://localhost/ClientMIS" + HttpContext.Current.Request.Url.PathAndQuery);}
else{
myReq = (
HttpWebRequest)WebRequest.Create("http://localhost" + HttpContext.Current.Request.Url.PathAndQuery);}
try{
WebResponse responseObj = myReq.GetResponse(); HttpContext.Current.Response.ContentType = responseObj.ContentType; if (!responseObj.ContentType.ToUpper().Contains("image".ToUpper())){
StreamReader srObjResponse = new StreamReader (responseObj.GetResponseStream()); HttpContext.Current.Response.Write(srObjResponse.ReadToEnd());}
else{
Stream s = responseObj.GetResponseStream(); long lPosition=0; while (lPosition < responseObj.ContentLength){
byte[] buffer = new byte[Math.Min(5000, responseObj.ContentLength - lPosition)];s.Read(buffer, 0, (
int)Math.Min(5000, responseObj.ContentLength - lPosition)); HttpContext.Current.Response.OutputStream.Write(buffer, 0, (int)Math.Min(5000, responseObj.ContentLength - lPosition)); HttpContext.Current.Response.Flush();lPosition += 5000;
}
}
HttpContext.Current.Response.End();}
catch (Exception ex){
}
}
public bool IsReusable{
get{
return true;}
}
#endregion
}
and
<
httpHandlers><
add verb="*" path="*" type="TestCommunication.CommunicationHandler"/> httpHandlers>in web.config
Replies
Know the answer? Post it — somebody with the same question will find it here.