Category Archives: bing-api

Reading Apache entity as a String [duplicate]

This question already has an answer here:

Currently, I am suing Bing search API which uses Apache httpcomponents.

Using the default sample code, provided by Microsoft, I will be able to get a long and messy string corresponding to any given queries.

Here is the code and the output:

      URIBuilder builder = new URIBuilder("https://api.cognitive.microsoft.com/bing/v5.0/search");

        builder.setParameter("q", "stackoverflow");
        builder.setParameter("count", "10");
        builder.setParameter("offset", "0");
        builder.setParameter("mkt", "en-us");
        builder.setParameter("safesearch", "Moderate");

        URI uri = builder.build();
        HttpGet request = new HttpGet(uri);

        request.setHeader("Ocp-Apim-Subscription-Key", "09afdeaf63594d128b0fee675204eeaf");



        HttpClient httpclient = HttpClients.createDefault();

       // StringEntity reqEntity = new StringEntity("{body}");
       // request.setEntity(reqEntity); this line seems not to be available for version 4.5

        HttpResponse response = httpclient.execute(request);
        HttpEntity entity = response.getEntity();


        if (entity != null) 
        {

            System.out.println(EntityUtils.toString(entity));

        }

Output :

{"_type": "SearchResponse", "webPages": {"webSearchUrl": "https:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=QZrj_agC6cbJ0WoA1ROkPTLkl-rn22a0cuIH7fYows8&v=1&r=https%3a%2f%2fwww.bing.com%2fsearch%3fq%3dstackoverflow&p=DevEx,5337.1", "totalEstimatedMatches": 5740000, "value": [{"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.0", "name": "Stack Overflow - Official Site", "url": "http:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=ZtWYaKOoMm63yNx3BLG3-K77WwtV_J4Ap98RWuCT_k0&v=1&r=http%3a%2f%2fstackoverflow.com%2f&p=DevEx,5096.1", "about": [{"name": "Stack Overflow"}, {"name": "Stack Overflow"}], "displayUrl": "stackoverflow.com", "snippet": "A language-independent collaboratively edited question and answer site for programmers. Questions and answers displayed by user votes and tags.", "deepLinks": [{"name": "Questions", "url": "http:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=MWlej7Nw7RPnjXR9lsNcBMiztMdkV4dKjQNV0UIYpV8&v=1&r=http%3a%2f%2fstackoverflow.com%2fquestions&p=DevEx,5085.1", "snippet": "All Questions. newest 432 featured frequent votes active unanswered. 0. votes. 0answers 2 views Remove Custom Tooltip on mouseout Google Chart."}, {"name": "Stack Overflow Careers", "url": "http:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=pSXBxYi9PkNG8fGfuJHOn6ZE5VuDSOMbouBGQd677zc&v=1&r=http%3a%2f%2fcareers.stackoverflow.com%2f&p=DevEx,5086.1", "snippet": "We've hired multiple amazing developers that found us on Stack Overflow Careers. No wonder; practically ..."}, {"name": "Tour", "url": "http:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=gb8XgIFNdK4CLImOtoRb8VW7xhmQ90rHkpBNkpHlzMg&v=1&r=http%3a%2f%2fstackoverflow.com%2ftour&p=DevEx,5087.1", "snippet": "Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta ..."}, {"name": "Review", "url": "http:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=4ASmGAzJrabTKzyWZzIW3E1kUxo0gt0fEji6FpOmTgk&v=1&r=http%3a%2f%2fstackoverflow.com%2freview&p=DevEx,5088.1", "snippet": "Visit the review queues on Meta Stack Overflow. Help improve the site. Stack Overflow is moderated by you. Help your fellow ..."}, {"name": "Meta", "url": "http:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=gbVkIOwgKekz9tk0PL7OqVETmuJPRyX-FZdH-JXm3k0&v=1&r=http%3a%2f%2fmeta.stackoverflow.com%2f&p=DevEx,5089.1", "snippet": "Stack Overflow Meta Stack Overflow your communities . Sign up or log in to customize your list. more stack exchange communities company blog. Stack Exchange Inbox ..."}, {"name": "Unanswered", "url": "http:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=BMHElff1Auz_21G1anAduPnt4047D6aBqLun8O6S77s&v=1&r=http%3a%2f%2fstackoverflow.com%2funanswered&p=DevEx,5090.1", "snippet": "Unanswered Questions. my tags newest votes no answers all questions. 139. votes. 0answers 3k views Storing kinect's v2.0 motion to BVH file."}], "dateLastCrawled": "2016-07-28T22:49:00"}, {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.1", "name": "Stack Overflow Business | Stack Overflow", "url": "http:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=hsavmUWhVOYp_-m8g69d_PGpn2MEeLjrMtinH-kkvpg&v=1&r=http%3a%2f%2fbusiness.stackoverflow.com%2f&p=DevEx,5110.1", "displayUrl": "business.stackoverflow.com", "snippet": "Stack Overflow Business delivers solutions to fit your company's developer hiring needs. Our Talent and Ads products aim to help you hire the best tech talent.", "dateLastCrawled": "2016-07-26T21:11:00"}, {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.2", "name": "Stack Overflow Blog – A destination for all things related ...", "url": "https:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=Zw7Qj_LYTsy1_USWKCG4ur8BObrrMAByivF948Xn_Mo&v=1&r=https%3a%2f%2fblog.stackoverflow.com%2f&p=DevEx,5123.1", "displayUrl": "https:\/\/blog.stackoverflow.com", "snippet": "Welcome! We are Stack Overflow, home of Stack Exchange, Stack Overflow Careers, and over 100 Q&A sites. In addition to writing a lot of code, we like to write about ...", "dateLastCrawled": "2016-07-24T15:40:00"}, {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.3", "name": "Stack Overflow - Wikipedia, the free encyclopedia", "url": "https:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=Zyu9ssq8vYM9BtumzgoufwXezGwFPj-PsyB8OKACKmM&v=1&r=https%3a%2f%2fen.wikipedia.org%2fwiki%2fStack_Overflow&p=DevEx,5154.1", "about": [{"name": "Stack Overflow"}, {"name": "Stack overflow"}], "displayUrl": "https:\/\/en.wikipedia.org\/wiki\/Stack_Overflow", "snippet": "Stack Overflow is a privately held website, the flagship site of the Stack Exchange Network, created in 2008 by Jeff Atwood and Joel Spolsky. It was created to be a ...", "dateLastCrawled": "2016-07-25T07:25:00"}, {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.4", "name": "Stack Overflow", "url": "http:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=Aj63627f7AtXi0DvkmoLLlr1vQiTlWRSZriLKOJ0Npk&v=1&r=http%3a%2f%2fja.stackoverflow.com%2f&p=DevEx,5166.1", "displayUrl": "ja.stackoverflow.com", "snippet": "Stack Overflow; Server Fault; Super User; Web Applications; Ask Ubuntu; Webmasters; Game Development; TeX - LaTeX; Programmers; Unix & Linux; Ask Different (Apple ...", "dateLastCrawled": "2016-07-29T09:33:00"}, {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.5", "name": "Stack Overflow Careers - Hiring Developers | Stack Overflow", "url": "http:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=OJXAHD7AbsDohce_m2W7Ufi6b8kMTdmccWZl2tHrhhw&v=1&r=http%3a%2f%2fwww.stackoverflowbusiness.com%2fcareers%2fus%2f&p=DevEx,5180.1", "displayUrl": "www.stackoverflowbusiness.com\/careers\/us", "snippet": "Hiring developers is hard. Stack Overflow Careers brings your entire developer hiring process together. Reach the right talent, right now.", "dateLastCrawled": "2016-07-28T10:49:00"}, {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.6", "name": "www.careers.stackoverflow.com", "url": "https:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=qSOuqbfejnr-o3yaCYAL8MmMJSGX7tSLX-pwO3ma16w&v=1&r=https%3a%2f%2fwww.careers.stackoverflow.com%2fsimonkrenger&p=DevEx,5191.1", "displayUrl": "https:\/\/www.careers.stackoverflow.com\/simonkrenger", "snippet": "Object Moved This document may be found here", "dateLastCrawled": "2016-07-16T01:30:00"}, {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.7", "name": "all rooms | chat.stackoverflow.com", "url": "https:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=8AUKoAF3REzPCYH1cnUkfcNAiQi3Olz0k-LK9FNA4lI&v=1&r=https%3a%2f%2fchat.stackoverflow.com%2f&p=DevEx,5204.1", "displayUrl": "https:\/\/chat.stackoverflow.com", "snippet": "a list of all rooms, recently active rooms first ... Now At A Stack Overflow Near You: http:\/\/stackoverflow.com\/documentation. 55s ago – cswl. 13.3k. 531. info. Python.", "dateLastCrawled": "2016-07-28T07:26:00"}, {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.8", "name": "What is stack overflow? - Definition from WhatIs.com", "url": "http:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=9TdNMebJCwC29zg_q_sKlAkHQix08F0_RFCEQXp56MU&v=1&r=http%3a%2f%2fwhatis.techtarget.com%2fdefinition%2fstack-overflow&p=DevEx,5217.1", "displayUrl": "whatis.techtarget.com\/definition\/stack-overflow", "snippet": "A stack overflow is an undesirable condition in which a particular computer program tries to use more memory space than the call stack has available. In programming ...", "dateLastCrawled": "2016-07-29T20:44:00"}, {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.9", "name": "How to Troubleshoot Windows Internal Stack Overflow Error ...", "url": "https:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=BJke1-esz0kMRLHq1BzB6tNg495aweYscbcbF9R7u3Q&v=1&r=https%3a%2f%2fsupport.microsoft.com%2fen-us%2fkb%2f145799&p=DevEx,5230.1", "displayUrl": "https:\/\/support.microsoft.com\/en-us\/kb\/145799", "snippet": "This article lists steps to help you troubleshoot problems with stack overflow errors in Windows. Stacks are reserved memory that programs use to process ...", "dateLastCrawled": "2016-07-27T13:40:00", "searchTags": [{"name": "search.description", "content": "\"This article lists steps to help you troubleshoot problems with stack overflow errors in Windows. Stacks are reserved memory that programs use to process hardware events. A stack overflow occurs when there is not enough space in memory to run the...\"; this; article; lists; steps; to; help; you; troubleshoot; problems; with; stack; overflow; errors; in; windows; stacks; are; reserved; memory; that; programs; use; to; process; hardware; events; a; stack; overflow; occurs; when; there; is; not; enough; space; in; memory; to; run; the"}, {"name": "search.kbparents", "content": "\"6912 6898 7482 6713 6513 1139 10435 7864 9980\"; 6912; 6898; 7482; 6713; 6513; 1139; 10435; 7864; 9980"}, {"name": "search.datemodified", "content": "\"1\/19\/2007 5:50:50 PM\"; 1; 19; 2007; 5; 50; 50; pm"}, {"name": "search.contenttype", "content": "\"kb\"; kb"}, {"name": "search.products", "content": "\"230209 185507 357152\"; 230209; 185507; 357152"}, {"name": "search.keywords", "content": "\"kbenv kberrmsg kbfaq kbhowto kbprod2web kbtshoot KB145799\"; kbenv; kberrmsg; kbfaq; kbhowto; kbprod2web; kbtshoot; kb145799"}]}]}, "relatedSearches": {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#RelatedSearches", "value": [{"text": "Stack Overflow Survey", "displayText": "Stack Overflow Survey", "webSearchUrl": "https:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=69NTrQam0MfGHXhYrQN1z9H5i6r1vzj6eZHkRKt_U_s&v=1&r=https%3a%2f%2fwww.bing.com%2fsearch%3fq%3dStack%2bOverflow%2bSurvey&p=DevEx,5338.1"}, {"text": "How to Fix Stack Overflow", "displayText": "How to Fix Stack Overflow", "webSearchUrl": "https:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=iDHLJibsH1NNGD43qCY1KegTIk8R_QWidIvSk637lA0&v=1&r=https%3a%2f%2fwww.bing.com%2fsearch%3fq%3dHow%2bto%2bFix%2bStack%2bOverflow&p=DevEx,5339.1"}, {"text": "Stack Overflow at Line 155", "displayText": "Stack Overflow at Line 155", "webSearchUrl": "https:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=1JP0FyWdqsxITOYKTvh0eQY7SSeuKsdgrDpIZWDtbyI&v=1&r=https%3a%2f%2fwww.bing.com%2fsearch%3fq%3dStack%2bOverflow%2bat%2bLine%2b155&p=DevEx,5340.1"}, {"text": "Free Stack Overflow Fix", "displayText": "Free Stack Overflow Fix", "webSearchUrl": "https:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=dZAcXUORP_ft3det2WS4nxLstG721a7ZYkuos81ZreY&v=1&r=https%3a%2f%2fwww.bing.com%2fsearch%3fq%3dFree%2bStack%2bOverflow%2bFix&p=DevEx,5341.1"}, {"text": "Stack Overflow at Line 2679", "displayText": "Stack Overflow at Line 2679", "webSearchUrl": "https:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=MzWrRGC4cDnCJitSycECyZ37Gao9RiOev0xhfTyZpNo&v=1&r=https%3a%2f%2fwww.bing.com%2fsearch%3fq%3dStack%2bOverflow%2bat%2bLine%2b2679&p=DevEx,5342.1"}, {"text": "Stack Overflow at Line 0 Error Message", "displayText": "Stack Overflow at Line 0 Error Message", "webSearchUrl": "https:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=iC6guwtAkrdIqZHE-vbOvlIIfzLZN5AqLk3pd1_X0Gg&v=1&r=https%3a%2f%2fwww.bing.com%2fsearch%3fq%3dStack%2bOverflow%2bat%2bLine%2b0%2bError%2bMessage&p=DevEx,5343.1"}, {"text": "Stack Overflow Careers", "displayText": "Stack Overflow Careers", "webSearchUrl": "https:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=ghTSmregIFzg7vfaHovOeX-3fBU_2_2ep-ytw_eoNGY&v=1&r=https%3a%2f%2fwww.bing.com%2fsearch%3fq%3dStack%2bOverflow%2bCareers&p=DevEx,5344.1"}, {"text": "Stack Overflow at Line 2", "displayText": "Stack Overflow at Line 2", "webSearchUrl": "https:\/\/www.bing.com\/cr?IG=9F94ED9739924F76AE0FE64EC4EEB7AD&CID=13FCE7A59F5B6F7D2B81EEC79E9B6E1E&rd=1&h=LBlo8uZQc1bp9fefhLWBEakyFBTQ83K8RYNw3sTT-AU&v=1&r=https%3a%2f%2fwww.bing.com%2fsearch%3fq%3dStack%2bOverflow%2bat%2bLine%2b2&p=DevEx,5345.1"}]}, "rankingResponse": {"mainline": {"items": [{"answerType": "WebPages", "resultIndex": 0, "value": {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.0"}}, {"answerType": "WebPages", "resultIndex": 1, "value": {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.1"}}, {"answerType": "WebPages", "resultIndex": 2, "value": {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.2"}}, {"answerType": "WebPages", "resultIndex": 3, "value": {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.3"}}, {"answerType": "WebPages", "resultIndex": 4, "value": {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.4"}}, {"answerType": "WebPages", "resultIndex": 5, "value": {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.5"}}, {"answerType": "WebPages", "resultIndex": 6, "value": {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.6"}}, {"answerType": "WebPages", "resultIndex": 7, "value": {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.7"}}, {"answerType": "WebPages", "resultIndex": 8, "value": {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.8"}}, {"answerType": "WebPages", "resultIndex": 9, "value": {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.9"}}, {"answerType": "RelatedSearches", "value": {"id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#RelatedSearches"}}]}}}

What I want is somehow simple. I want to extract the displayUrl but I do not know how.