Osirus
Osirus

Reputation: 264

How do I pass custom header through web browser for Windows Phone 8?

How do I pass custom header through web browser on the windows phone 8? Eg I need to pass 4 values which are

User_Model=Nokia 920
User_Device_Width=768
User_Name=USERNAME
User_Password=PASSWORD

In PHP this is how it looks like. Not sure how do I implement it into C# for Windows Phone 8 development.

<?php
$uri = 'http://test.local/index.php?action=123';
$ch = curl_init($uri);
curl_setopt_array($ch, array(
    CURLOPT_HTTPHEADER  => array('USER_Model:Nokia 920', 'USER_DEVICE_WIDTH:768', 'USER_NAME:USERNAME', 'USER_PASSWORD:PASSWORD'),
    CURLOPT_RETURNTRANSFER  =>true,
    CURLOPT_VERBOSE     => 1
));
$out = curl_exec($ch);
curl_close($ch);
// echo response output?>

<!DOCTYPE HTML>
<html>
<head>
    <title>Test</title>
</head>
<body>
    <div>
        <?php echo $out; ?>
    </div>
    <div id="placeholder"></div>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
        $(function() {
            var xhr;
            try{
                xhr = new XMLHttpRequest();
            }catch (e){
                try{
                    xhr = new XDomainRequest();
                } catch (e){
                    try{
                        xhr = new ActiveXObject('Msxml2.XMLHTTP');
                    }catch (e){
                        try{
                            xhr = new ActiveXObject('Microsoft.XMLHTTP');
                        }catch (e){
                            alert('\nYour browser is not' + ' compatible with XHR2');                           
                        }
                    }
                }
            }               
        });
    </script>
</body> 
</html>

Thanks in advance!

Upvotes: 2

Views: 1723

Answers (2)

Osirus
Osirus

Reputation: 264

After months of trying to sort out how this could actually be done, I've finally managed to add the headers to the web browser.

So here is what I did.

private bool firstload = true; 
//Define the headers
string AdditionalHeaders = "USER_MODEL:" + phoneModel + "USER_DEVICE_WIDTH:" + deviceWidth + "USER_NAME:" + USERNAME + "\r\User_Password=" + PASSWORD; 

public void loadsite() 
{ 
  webBroswer.Navigate(new Uri("http://www.xhaus.com/headers"), null, AdditionalHeaders); 
  webBrowser.Navigating += webBrowser_Navigating; 
} 

//  This is done so that when navigating to any pages, the header will still be attached to the webbrowser.
void webBroswer_Navigating(object sender, NavigatingEventArgs e) 
{ 
  if (firstload == true) 
    { 
      // This is to prevent it from looping the same page
      firstload = false; 
    } 
  else 
  { 
    string url = e.Uri.ToString(); 

    webBroswer.Navigate(new Uri(url, Urikind.Absolute), null, AdditionalHeaders); 
    firstload= true;
  }
}

I'm not sure if it's done correctly but never the less, it worked for my project.

Upvotes: 3

Vova Bilyachat
Vova Bilyachat

Reputation: 19474

xhr have method

void setRequestHeader(
   DOMString header,
   DOMString value
);

So simply

xhr.setRequestHeader("Name", "value");

More info about xhr methods

Upvotes: 1

Related Questions