1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Text ;
4+ using System . Net ;
5+
6+ namespace ConsoleAppPost
7+ {
8+ class Program
9+ {
10+ // Replace the accessKey string value with your valid access key.
11+ const string accessKey = "enter key here" ;
12+
13+ // The endpoint URI.
14+ const string uriBase = "https://api.cognitive.microsoft.com/bing/v7.0/images/details" ;
15+
16+ // The image to upload. Replace with your file and path.
17+ const string imageFile = "your-image-file.jpg" ;
18+
19+ // Used to return image results including relevant headers
20+ struct SearchResult
21+ {
22+ public String jsonResult ;
23+ public Dictionary < String , String > relevantHeaders ;
24+ }
25+
26+ static void Main ( )
27+ {
28+ Console . OutputEncoding = System . Text . Encoding . UTF8 ;
29+
30+ if ( accessKey . Length == 32 )
31+ {
32+ //Console.WriteLine("Searching images for: " + searchTerm);
33+
34+ SearchResult result = BingImageSearch ( uriBase ) ;
35+
36+ Console . WriteLine ( "\n Relevant HTTP Headers:\n " ) ;
37+ foreach ( var header in result . relevantHeaders )
38+ Console . WriteLine ( header . Key + ": " + header . Value ) ;
39+
40+ Console . WriteLine ( "\n JSON Response:\n " ) ;
41+ Console . WriteLine ( JsonPrettyPrint ( result . jsonResult ) ) ;
42+ }
43+ else
44+ {
45+ Console . WriteLine ( "Invalid Bing Search API subscription key!" ) ;
46+ Console . WriteLine ( "Please paste yours into the source code." ) ;
47+ }
48+
49+ Console . Write ( "\n Press Enter to exit " ) ;
50+ Console . ReadLine ( ) ;
51+ }
52+
53+ /// <summary>
54+ /// Performs a Bing Image search and return the results as a SearchResult.
55+ /// </summary>
56+ static SearchResult BingImageSearch ( string uriStr )
57+ {
58+ WebClient client = new WebClient ( ) ;
59+ client . Headers [ "Ocp-Apim-Subscription-Key" ] = accessKey ;
60+ client . Headers [ "ContentType" ] = "multipart/form-data" ;
61+
62+ byte [ ] resp = client . UploadFile ( uriBase + "?modules=All" , imageFile ) ;
63+ var json = System . Text . Encoding . Default . GetString ( resp ) ;
64+
65+ // Create result object for return
66+ var searchResult = new SearchResult ( )
67+ {
68+ jsonResult = json ,
69+ relevantHeaders = new Dictionary < String , String > ( )
70+ } ;
71+
72+ return searchResult ;
73+ }
74+
75+ /// <summary>
76+ /// Formats the given JSON string by adding line breaks and indents.
77+ /// </summary>
78+ /// <param name="json">The raw JSON string to format.</param>
79+ /// <returns>The formatted JSON string.</returns>
80+ static string JsonPrettyPrint ( string json )
81+ {
82+ if ( string . IsNullOrEmpty ( json ) )
83+ return string . Empty ;
84+
85+ json = json . Replace ( Environment . NewLine , "" ) . Replace ( "\t " , "" ) ;
86+
87+ StringBuilder sb = new StringBuilder ( ) ;
88+ bool quote = false ;
89+ bool ignore = false ;
90+ char last = ' ' ;
91+ int offset = 0 ;
92+ int indentLength = 2 ;
93+
94+ foreach ( char ch in json )
95+ {
96+ switch ( ch )
97+ {
98+ case '"' :
99+ if ( ! ignore ) quote = ! quote ;
100+ break ;
101+ case '\\ ' :
102+ if ( quote && last != '\\ ' ) ignore = true ;
103+ break ;
104+ }
105+
106+ if ( quote )
107+ {
108+ sb . Append ( ch ) ;
109+ if ( last == '\\ ' && ignore ) ignore = false ;
110+ }
111+ else
112+ {
113+ switch ( ch )
114+ {
115+ case '{' :
116+ case '[' :
117+ sb . Append ( ch ) ;
118+ sb . Append ( Environment . NewLine ) ;
119+ sb . Append ( new string ( ' ' , ++ offset * indentLength ) ) ;
120+ break ;
121+ case '}' :
122+ case ']' :
123+ sb . Append ( Environment . NewLine ) ;
124+ sb . Append ( new string ( ' ' , -- offset * indentLength ) ) ;
125+ sb . Append ( ch ) ;
126+ break ;
127+ case ',' :
128+ sb . Append ( ch ) ;
129+ sb . Append ( Environment . NewLine ) ;
130+ sb . Append ( new string ( ' ' , offset * indentLength ) ) ;
131+ break ;
132+ case ':' :
133+ sb . Append ( ch ) ;
134+ sb . Append ( ' ' ) ;
135+ break ;
136+ default :
137+ if ( quote || ch != ' ' ) sb . Append ( ch ) ;
138+ break ;
139+ }
140+ }
141+ last = ch ;
142+ }
143+
144+ return sb . ToString ( ) . Trim ( ) ;
145+ }
146+ }
147+ }
0 commit comments