none
New Azure Bing API for Java - Error java.net.UnknownHostException: api.datamarket.azure.com

    Question

  • I'm trying to use the code that was posted under another question regarding the New Azure Bing API for Java and I can’t get the connection to work.  The error I receive is java.net.UnknownHostException: api.datamarket.azure.com  I'm not sure what's wrong and any help is really appreciated.

    My code is below which I copied from another message in the forum but can't get it to work. Am I missing something? 

    import java.io.*;
    import java.net.*;


    import org.apache.commons.codec.binary.Base64;
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.BasicResponseHandler;
    import org.apache.http.impl.client.DefaultHttpClient;
    //import org.json.simple.JSONObject;
    import net.sf.json.*;
    import net.sf.json.util.*;
    import net.sf.json.JSONObject;





    import java.io.IOException;



    public class bingSearchSimple {

    public static void main(String[] args) {
    //public static void getBing() throws Exception {



    String accountKey = ":key";
             byte[] accountKeyBytes = Base64.encodeBase64(accountKey.getBytes());
             String accountKeyEnc = new String(accountKeyBytes);

             String bingURL = "https://api.datamarket.azure.com/Bing/SearchWeb/v1/Web?Query=%27xbox%27&$format=json";
                 
             
       try {
                 URL url = new URL(bingURL);
                 JSONObject result = null;
                 StringBuilder sb = new StringBuilder();
                 
                 
                 URLConnection urlConnection = url.openConnection();
                 urlConnection.setRequestProperty("Authorization", "Basic " + accountKeyEnc);
                 InputStream is = urlConnection.getInputStream();  //PROGRAM ERRORS OUT AT THIS POINT
                 System.out.println("Input stream "+is);
                 InputStreamReader isr = new InputStreamReader(is);

                 int numCharsRead;
                 char[] charArray = new char[1024];
                
                 while ((numCharsRead = isr.read(charArray)) > 0) {
                     sb.append(charArray, 0, numCharsRead);
                    
                 }
                 result = JSONObject.fromObject(sb.toString());
                 
                                        
                             
             } catch (MalformedURLException e) {
                 System.out.println("malformed URL Exception "+ e);
                 
            e.printStackTrace();
             } catch (IOException e) {
            System.out.println("IO Exception " +e);
                
            e.printStackTrace();
             }       





    }

    }

    Thursday, July 11, 2013 12:50 PM

Answers

  • Thanks for the help everyone, The issue was actually due to the network.  That's why the connection wasn't established.  I tried our guest network and was able to do the search.  So in the future anyone who has an issue with   InputStream is = urlConnection.getInputStream();  and you don't get any return code or message with a java.net.UnknownHostException: api.datamarket.azure.com error.  Try another network or changing firewall settings to all the connection.

    thanks,

    Ryan

    • Marked as answer by p90xfan Monday, July 15, 2013 5:43 PM
    Monday, July 15, 2013 5:43 PM

All replies

  • Hi,

    Try this query url: https://api.datamarket.azure.com/Bing/Search/v1/Web?Query=%27xbox%27

    You can test the query from service explorer: https://datamarket.azure.com/dataset/explore/bing/search

    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, July 12, 2013 3:52 AM
  • Thanks for the help I've been really stuck on this.  I changed the url as suggested but still have the same error.  It seems like the connection is never established.  I attached the stack trace below.

    java.net.UnknownHostException: api.datamarket.azure.com

    1. at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
      at java.net.Socket.connect(Socket.java:529)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:564)
      at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
      at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
      at sun.net.www.http.HttpClient.openServer(HttpClient.java:395)
      at sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
      at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:272)
      at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)
      at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
      at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
      at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
      at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)

    Friday, July 12, 2013 11:45 AM
  • Hi,

    Try this:

    String accountKey = "accountKey";      //no ':'
    byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes());

    http://learn-it-stuff.blogspot.in/2012/09/using-bing-custom-search-inside-your.html

    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.

    Monday, July 15, 2013 3:14 AM
  • Thanks for the help everyone, The issue was actually due to the network.  That's why the connection wasn't established.  I tried our guest network and was able to do the search.  So in the future anyone who has an issue with   InputStream is = urlConnection.getInputStream();  and you don't get any return code or message with a java.net.UnknownHostException: api.datamarket.azure.com error.  Try another network or changing firewall settings to all the connection.

    thanks,

    Ryan

    • Marked as answer by p90xfan Monday, July 15, 2013 5:43 PM
    Monday, July 15, 2013 5:43 PM