Android Code Snippet Set HTTP Request Header with OkHttpUtils2

Discussion in 'Code Snippets' started by Erel, Mar 30, 2014.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Description: The following code allows you to set the request headers before the request is sent. This is useful for example with sites that block requests without a User-Agent header.

    This code depends on HttpUtils2 and Http libraries:
    Code:
    Dim j As HttpJob
    j.Initialize(
    "job name", Me)
    j.Download(<link>) 
    'it can also be PostString or any of the other methods
    j.GetRequest.SetHeader("User-Agent""Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0")
    It is a bit confusing as it seems that the header is set after the request was sent. However internally HttpJob uses CallSubDelayed to send the request. So the request will only be sent after the current code execution completes.

    Note that you can set any number of headers.

    The content type header should be set with SetContentType.

    Tags: http headers, httputils2, user agent
     
    Last edited: Sep 17, 2018
    ocalle, MikeH, joseluis and 4 others like this.
  2. DonManfred

    DonManfred Expert Licensed User

    You forgot to TAG this, @Erel :D
     
    Peter Simpson likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    Fixed.
     
  4. Peter Simpson

    Peter Simpson Expert Licensed User

    That's great to know @Erel. So I'm presuming that the following line will work a treat then...

    Code:
    AutoUpdateIP.GetRequest.Timeout = DateTime.TicksPerSecond * 45
    AutoUpdateIP.GetRequest.SetHeader(
    "User-Agent""Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.92 Safari/537.36")
     
    jinyistudio likes this.
  5. Erel

    Erel Administrator Staff Member Licensed User

    Yes. As long you first call the "download" method.
     
    Peter Simpson likes this.
  6. Nerdworld

    Nerdworld Member Licensed User

    I'm currently trying to set some headers this way, but i'm getting this error:

    Request.GetRequest().SetHeader("User-Agent", "SomeTesting")

    Unkown type: anywheresoftware.b4a.http.HttpClientWrapper.HttpUriRequestWrapper
    Are you missing a library reference?

    Is this coming from a version difference, or am i missing something?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    You need to add a reference to Http library.
     
  8. Nerdworld

    Nerdworld Member Licensed User

    Aaaaaah, merci! ;)
     
  9. deantangNYP

    deantangNYP Active Member Licensed User

    May i know how to add into the Header the following
    - Basic authorisation
    - Username, password (base64 encoded)

    THANKS !!
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    You can set Job.Username and Job.Password properties.
     
  11. seanfogg

    seanfogg Member Licensed User

    Does this work with the PostString method also?
     
  12. DonManfred

    DonManfred Expert Licensed User

    yes (i guess)
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    That's true. It works with all methods.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice