none
How can I log in through a proxy server to perform searches on the windows azure marketplace bing api

    Question

  • I am trying search the web with a particular search string and return the urls of webpages where the search string was found. I am using the bing api Windows azure marketplace. But the problem is my school uses a proxy server that requires a username and password. I have valid credentials but i don't know have to log in programmatically to the proxy server using c#. Could I please get a sample of how to connect to the bing api through a proxy server?

    Here is what i have done so far

    static void Main(string[] args)
            {
                try
                {

                const string bingkey = "my id";

                bing.Credentials = new NetworkCredential(bingkey, bingkey);

                var results = SearchAsync("search string");

               // IWebProxy proxy2 = HttpWebRequest.DefaultWebProxy;

                int i =0; 

                foreach (var result in results.Result)
                {
                    Console.WriteLine(result.Url);
                    ++i; 
                }

                Console.WriteLine(i);
            }
            catch (WebException e)
            {
                Console.WriteLine(e.ToString());
            }
            catch (DataServiceClientException e)
            {
                Console.WriteLine(e.ToString());
            }
            catch (Exception e)
    Thursday, June 27, 2013 10:51 AM

Answers

  • Here is a suggested solution to the problem I had: 

     

    IWebProxy proxy = new WebProxy("proxy server address", port number);
                    proxy.Credentials = new NetworkCredential("username", "password");

                    WebClient client = new WebClient();
                    client.Proxy = proxy;

                    string searchItem = "search item";

                    BingRequest request = new BingRequest();
                    request.SearchTerm = searchItem;

                    client.Credentials = request.Credentials;
                    // pass in the bing api url 
                    string response = client.DownloadString(request.Url);

                    IEnumerable<WebResult> results = request.parse(response);

                    foreach (WebResult result in results)
                    {
                        Console.WriteLine(result.Url);
                    }
                }
                catch (WebException e)
                {
                    Console.WriteLine(e.ToString()); 
                }

                Console.ReadKey(); 

    NB: I created my own Bing request class called BingRequest and a WebResult class 

     public class BingRequest
        {
            private const string AppID = "your app id";
            private string searchTerm;

            public string SearchTerm
            {
                get { return searchTerm; }
                set { searchTerm = value; }
            } 

            public string Url
            {
                get
                {
                    return string.Format("https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27web%27&Query=%27{0}%27", SearchTerm); 
                }
            }

            public ICredentials Credentials
            {
                get 
                {
                    return new NetworkCredential(AppID, AppID); 
                }
            }
            public IEnumerable<WebResult> parse(string xml)
            {
                XElement respXml = XElement.Parse(xml);
                XNamespace d = XNamespace.Get("http://schemas.microsoft.com/ado/2007/08/dataservices");
                XNamespace m = XNamespace.Get("http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");

                IEnumerable<WebResult> result = from item in respXml.Descendants(m + "properties")
                                                select new WebResult
                                                {
                                                    Title = new string(item.Element(d + "Title").Value.ToArray()),
                                                    Url = item.Element(d + "Url").Value,
                                                    Description = item.Element(d + "Description").Value
                                                }; 
                return result; 
            }
        }

    public class WebResult
        {
            private string title;
            private string url;
            private string description;

            public string Title
            {
                get { return title; }
                set { title = value; }
            }
           
            public string Url
            {
                get { return url; }
                set { url = value; }
            }
            
            public string Description
            {
                get { return description; }
                set { description = value; }
            } 
        }


    • Marked as answer by Anwuna Wednesday, July 03, 2013 1:22 PM
    Wednesday, July 03, 2013 1:21 PM

All replies

  • Hi,

    Try to set the <defaultProxy> element in config file:

    http://msdn.microsoft.com/en-us/library/kd3cf2ex.aspx

    Thanks,


    QinDian Tang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 28, 2013 5:23 AM
    Moderator
  • @QinDian Tang 

    I already did that but it still did not solve my problem. But I have found a way around it. Thanks. 

    Tuesday, July 02, 2013 12:48 AM
  • Hi,

    Would you mind share the solution with other community?

    Thanks,


    QinDian Tang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 02, 2013 1:44 AM
    Moderator
  • Yes I will. Thanks 

    Tuesday, July 02, 2013 6:46 AM
  • Here is a suggested solution to the problem I had: 

     

    IWebProxy proxy = new WebProxy("proxy server address", port number);
                    proxy.Credentials = new NetworkCredential("username", "password");

                    WebClient client = new WebClient();
                    client.Proxy = proxy;

                    string searchItem = "search item";

                    BingRequest request = new BingRequest();
                    request.SearchTerm = searchItem;

                    client.Credentials = request.Credentials;
                    // pass in the bing api url 
                    string response = client.DownloadString(request.Url);

                    IEnumerable<WebResult> results = request.parse(response);

                    foreach (WebResult result in results)
                    {
                        Console.WriteLine(result.Url);
                    }
                }
                catch (WebException e)
                {
                    Console.WriteLine(e.ToString()); 
                }

                Console.ReadKey(); 

    NB: I created my own Bing request class called BingRequest and a WebResult class 

     public class BingRequest
        {
            private const string AppID = "your app id";
            private string searchTerm;

            public string SearchTerm
            {
                get { return searchTerm; }
                set { searchTerm = value; }
            } 

            public string Url
            {
                get
                {
                    return string.Format("https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27web%27&Query=%27{0}%27", SearchTerm); 
                }
            }

            public ICredentials Credentials
            {
                get 
                {
                    return new NetworkCredential(AppID, AppID); 
                }
            }
            public IEnumerable<WebResult> parse(string xml)
            {
                XElement respXml = XElement.Parse(xml);
                XNamespace d = XNamespace.Get("http://schemas.microsoft.com/ado/2007/08/dataservices");
                XNamespace m = XNamespace.Get("http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");

                IEnumerable<WebResult> result = from item in respXml.Descendants(m + "properties")
                                                select new WebResult
                                                {
                                                    Title = new string(item.Element(d + "Title").Value.ToArray()),
                                                    Url = item.Element(d + "Url").Value,
                                                    Description = item.Element(d + "Description").Value
                                                }; 
                return result; 
            }
        }

    public class WebResult
        {
            private string title;
            private string url;
            private string description;

            public string Title
            {
                get { return title; }
                set { title = value; }
            }
           
            public string Url
            {
                get { return url; }
                set { url = value; }
            }
            
            public string Description
            {
                get { return description; }
                set { description = value; }
            } 
        }


    • Marked as answer by Anwuna Wednesday, July 03, 2013 1:22 PM
    Wednesday, July 03, 2013 1:21 PM