How to build http request and add a header to the heder collection

Last post 08-07-2008 4:34 PM by docluv. 7 replies.

Sort Posts:

  • How to build http request and add a header to the heder collection

    08-01-2008, 3:19 PM
    • Member
      point Member
    • 287867
    • Member since 08-01-2008, 7:11 PM
    • Posts 4

    Hello everyone,

    Could you please help me with this issue it is urgent.
    I am traying to call a page http://www.whatevers.com but I need to build the request and to add some new values to the header collection because this servers need that value in the request.
    I want that once the request is made the browser jumps directly to that url too

    Could you help me with this

    Thanks in advance 

    Armando 

     

     

     

     

  • Re: How to build http request and add a header to the heder collection

    08-02-2008, 4:37 AM

    u can add a HttpModule to trap the request and make the changes and forward request again

    Mark as answer if you are agree with the solution. This helps the community.

    Sandeep Bhutani
  • Re: How to build http request and add a header to the heder collection

    08-05-2008, 8:02 AM
    • Member
      point Member
    • 287867
    • Member since 08-01-2008, 7:11 PM
    • Posts 4

    Hello,

    Thanks for your answer I just did that, and I also registered the module in the Web.config, when I run the code the following error comes out:

    Error Line: ---------   Dim headers As NameValueCollection = response.Headers
    Error Description--  This operation requires IIS integrated pipeline mode.

    At the end of my message I paste the portion of my code for you to see.

    I try also with: response.AddHeader("variable","value") and it do not send the header in the response.

    Thanks very much in advance for your help

    Armando

    -----Code ---- 

    Public Class MyHttpHandler
    Implements IHttpModule

    Public Sub Init(ByVal application As HttpApplication) Implements IHttpModule.Init
       
    AddHandler application.BeginRequest, AddressOf Me.OnInit
    End Sub

    ' event handler.

    Private Sub OnInit(ByVal [source] As [Object], ByVal e As EventArgs)
      
    Dim application As HttpApplication = CType([source], HttpApplication)
      
    Dim response As HttpResponse = application.Context.Response

       Dim headers As NameValueCollection = response.Headers

       Dim t As Type = headers.GetType    Dim p As PropertyInfo = t.GetProperty("IsReadOnly", BindingFlags.Instance Or BindingFlags.IgnoreCase Or BindingFlags.NonPublic Or BindingFlags.FlattenHierarchy)

       p.SetValue(headers, False, Nothing)
       headers.Add(
    "USER_VAR", "MyName")

    End Sub

    Public Sub Dispose() Implements IHttpModule.Dispose
    End Sub End Class

     

  • Re: How to build http request and add a header to the heder collection

    08-05-2008, 12:28 PM
    • Star
      12,355 point Star
    • docluv
    • Member since 06-29-2002, 7:16 PM
    • Willow Spring NC
    • Posts 1,965
    • ASPInsiders
      TrustedFriends-MVPs

    You need to register an event handler with one of the ASP.NET pipeline events, I think in this case like AuthenticateRequest or somewhere in there. Then your event handler will execute your code to do the 301 redirect.

    Imports Microsoft.VisualBasic

    Public Class _301RedirectModuleInherits ExBaseModule

     

    Public Overrides Sub Dispose()

    MyBase.Dispose()

    End Sub

    Public Overrides Sub Init(ByVal context As System.Web.HttpApplication)

    MyBase.Init(context)

    AddHandler context.AuthorizeRequest, AddressOf Process301

    End Sub

    Public Sub Process301(ByVal sender As Object, ByVal e As EventArgs)

    Dim app As HttpApplication = CType(sender, HttpApplication)

    Dim ht As Hashtable = Get301Mappings()Dim sPath As String = _

    app.Context.Request.AppRelativeCurrentExecutionFilePath

    If Not IsNothing(ht(sPath)) Then

    app.Response.StatusCode = 301 ' make a permanent redirect

    app.Response.AddHeader("Location", ht(sPath).ToString)

    app.Response.End()

    End If

    End Sub

    Private Function Get301Mappings() As Hashtable

    Dim ht As New Hashtable

    ht.Add("~/aboutcompany.htm", "history.aspx")

    ht.Add("~/default.htm", "default.aspx")

    ht.Add("~/Dallas.aspx", "Texas.aspx")

    ht.Add("~/Chrysler.aspx", "Mercedes.aspx")

    Return ht

    End Function

    End Class

  • How to Add header variables to HttpResponse object

    08-05-2008, 7:46 PM
    • Member
      point Member
    • 287867
    • Member since 08-01-2008, 7:11 PM
    • Posts 4

    Hello,

    I tried the example you typed but it did not work for me. That's exactly what I want to do, just add header variables to a HttpResponse object in order to when this get the destination my header variable is added to the pipeline. 

    Could you please send me a more complete example?

     Thanks very much in advance

     Armando

     

  • Re: How to Add header variables to HttpResponse object

    08-06-2008, 6:25 PM
    • Star
      12,355 point Star
    • docluv
    • Member since 06-29-2002, 7:16 PM
    • Willow Spring NC
    • Posts 1,965
    • ASPInsiders
      TrustedFriends-MVPs
  • Re: How to Add header variables to HttpResponse object

    08-07-2008, 2:42 PM
    • Member
      point Member
    • 287867
    • Member since 08-01-2008, 7:11 PM
    • Posts 4

    Hi Chist,

    I did registered the module in the Web.Config but it does not work either. Let me say something, I have IIS version 5.1, I running on Windows XP and I have no idea if it can  affect the code generated.

    Do you have any other way to add to add headers to the response.

    Thank you very much,
    Armando

     

  • Re: How to Add header variables to HttpResponse object

    08-07-2008, 4:34 PM
    • Star
      12,355 point Star
    • docluv
    • Member since 06-29-2002, 7:16 PM
    • Willow Spring NC
    • Posts 1,965
    • ASPInsiders
      TrustedFriends-MVPs

    Even if you set a break point in the code you cannot step through it? You do realize you need to create your own map of URLs that need to return a 301 redirect header right? This is just an example to give you the basic framework and idea. You need to somehow do your own URLs.

Page 1 of 1 (8 items)