locked
XML Code Sample (InstantAnswer SourceType) RRS feed

  • Question

  • How can  I set proxy authentication I am using ISA server and having this error when running LiveSearchAPIs "The Remote Server Returned  an error(407) Proxy Authentication is required"
    Monday, December 1, 2008 4:25 PM

Answers

  • I think we got the the end of what can be done with generic advice.

     

    Error 502 is suspicious. I haven't seen it very often, so I looked it up: the HTTP documentation defines it as

     

    502 Bad Gateway
    The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request.

     

    There seems to be something not working properly between the proxy and our server.

     

    Can you send to [My nick without -MSFT] at microsoft dot com a full HTTP dump (that you can capture with tools like NetMon or Fiddler - the latter is preferred) of a failed request/response from your machine, and if possible from the proxy? Or could you send me the public IP address of your proxy so that I can check what happened on our side?

     

    I would like to understand what is happening and exclude that you are hitting an issue with the service.

     

    Thanks in advance and sorry for the trouble.

     

    --Alessandro

    Tuesday, December 2, 2008 6:22 AM

All replies

  • Did you try to set the default credentials?

     

    Code Snippet
    //Create a request for the referrer URL.
    WebRequest request = WebRequest.Create(FailedRequest.UrlReferrer);
    // If required by the server, set the credentials.
    request.Credentials = CredentialCache.DefaultCredentials;

     

     

     

    HTH

     

    --Alessandro

    Monday, December 1, 2008 4:46 PM
  • Proxy Authentication is solved by using this code...



     System.Net.WebProxy rssProxy = new System.Net.WebProxy("http://***********:8080/");
                rssProxy.Credentials = new System.Net.NetworkCredential("****", "***");
                // bypass the proxy on local network
                rssProxy.BypassProxyOnLocal = true;
                System.Net.GlobalProxySelection.Select = rssProxy;



    but now I get this error 

    "The Remote Server returned an error(502) bad gateway"
    Monday, December 1, 2008 8:21 PM
  • I think we got the the end of what can be done with generic advice.

     

    Error 502 is suspicious. I haven't seen it very often, so I looked it up: the HTTP documentation defines it as

     

    502 Bad Gateway
    The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request.

     

    There seems to be something not working properly between the proxy and our server.

     

    Can you send to [My nick without -MSFT] at microsoft dot com a full HTTP dump (that you can capture with tools like NetMon or Fiddler - the latter is preferred) of a failed request/response from your machine, and if possible from the proxy? Or could you send me the public IP address of your proxy so that I can check what happened on our side?

     

    I would like to understand what is happening and exclude that you are hitting an issue with the service.

     

    Thanks in advance and sorry for the trouble.

     

    --Alessandro

    Tuesday, December 2, 2008 6:22 AM
  • 172.16.0.1:8080
    Tuesday, December 2, 2008 2:49 PM
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Xml;

    namespace HelloWorld
    {
        static class Program
        {

            const string AppId = "1900CC013CF979C8C0B80FE801D1C94C41E04A21";

            static void Main(string[] args)
            {

                HttpWebRequest request = BuildRequest();


                try
                {

                    // Send the request; display the response.
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    DisplayResponse(response);

                }
                catch (WebException ex)
                {
                    // An exception occurred while accessing the network.
                    Console.WriteLine(ex.Message);
                }

            }

            static HttpWebRequest BuildRequest()
            {

                System.Net.WebProxy rssProxy = new System.Net.WebProxy("http://172.16.0.1:8080/");
                rssProxy.Credentials = new System.Net.NetworkCredential("***", "***");
                // bypass the proxy on local network
                rssProxy.BypassProxyOnLocal = true;
                System.Net.GlobalProxySelection.Select = rssProxy;


                string requestString = "http://api.search.live.net/xml.aspx?"

                    // Common request fields (required)
                    + "AppId=" + AppId
                    + "&Query=calories in a squirrel"
                    + "&Sources=InstantAnswer"

                    // Common request fields (optional)
                    + "&Version=2.0"
                    + "&Market=en-us";

                // Create and initialize the request.
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestString);
                return request;
            }

            static void DisplayResponse(HttpWebResponse response)
            {
                // Load the response into an XmlDocument.
                XmlDocument document = new XmlDocument();
                document.Load(response.GetResponseStream());

                // Add the default namespace to the namespace manager.
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(
                    document.NameTable);
                nsmgr.AddNamespace(
                    "api",
                    "http://schemas.microsoft.com/LiveSearch/2008/04/XML/element");

                XmlNodeList errors = document.DocumentElement.SelectNodes(
                    "./api:Errors/api:Error",
                    nsmgr);

                if (errors.Count > 0)
                {
                    // There are errors in the response. Display error details.
                    DisplayErrors(errors);
                }
                else
                {
                    // There were no errors in the response. Display the
                    // InstantAnswer results.
                    DisplayResults(document.DocumentElement, nsmgr);
                }
            }

            static void DisplayResults(XmlNode root, XmlNamespaceManager nsmgr)
            {
                string version = root.SelectSingleNode("./@Version", nsmgr).InnerText;
                string searchTerms = root.SelectSingleNode(
                    "./api:Query/apiTongue TiedearchTerms",
                    nsmgr).InnerText;

                // Display the results header.
                Console.WriteLine("Live Search API Version " + version);
                Console.WriteLine("InstantAnswer results for " + searchTerms);
                Console.WriteLine();

                // Add the InstantAnswer SourceType and Encarta namespaces to the
                // namespace manager.
                string instantAnswerNamespaceUri =
                    "http://schemas.microsoft.com/LiveSearch/2008/04/XML/"
                    + "instantanswer";
                nsmgr.AddNamespace("ia", instantAnswerNamespaceUri);
                nsmgr.AddNamespace(
                    "enc",
                    "http://schemas.microsoft.com/LiveSearch/2008/04/XML/encarta");

                XmlNodeList results = root.SelectNodes(
                    "./ia:InstantAnswer/ia:Results/ia:InstantAnswerResult",
                    nsmgr);

                // Display the InstantAnswer results.
                foreach (XmlNode result in results)
                {
                    Console.WriteLine(
                        result.SelectSingleNode("./ia:Title", nsmgr).InnerText);
                    Console.WriteLine(
                        result.SelectSingleNode("./ia:Url", nsmgr).InnerText);

                    // This data may contain HTML line break tags. Replace these tags
                    // with the line terminator string.
                    string value =
                        "./ia:InstantAnswerSpecificData/enc:Encarta/enc:Value";
                    value = result.SelectSingleNode(value, nsmgr).InnerText;
                    value = System.Text.RegularExpressions.Regex.Replace(
                        value,
                        @"<[Bb][Rr]\s*/*\s*>",
                        Console.Out.NewLine);
                    Console.WriteLine(value);

                    Console.WriteLine();
                }
            }
            static void DisplayErrors(XmlNodeList errors)
            {
                // Iterate over the list of errors and display error details.
                Console.WriteLine("Errors:");
                Console.WriteLine();
                foreach (XmlNode error in errors)
                {
                    foreach (XmlNode detail in error.ChildNodes)
                    {
                        Console.WriteLine(detail.Name + ": " + detail.InnerText);
                    }

                    Console.WriteLine();
                }
            }


        }
    }

    Tuesday, December 2, 2008 2:58 PM