-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bing.cs
78 lines (63 loc) · 2.78 KB
/
Bing.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// <copyright file="Bing.cs" company="Air Osprey">
// MIT License (MIT). All rights reserved
// </copyright>
// <author>Larry Conklin</author>
// <summary>US Address Parsing class.</summary>
namespace USGeoAddressParser
{
using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
internal class Bing
{
private string userBingMapKey = "";
public HttpWebRequest WebGet()
{
string url = @"http://dev.virtualearth.net/REST/v1/Locations/US/OK/74012/tulsa/4705 south 129th east avenue?o=json&key=" + this.userBingMapKey;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
return request;
}
public void WebReponse()
{
Stream resStream = null;
const int MaxReadCharacters = 5125; // 5k
StringBuilder sb = new StringBuilder();
WebResponse response = this.WebGet().GetResponse();
resStream = this.WebGet().GetResponse().GetResponseStream();
StreamReader reader = new StreamReader(resStream, Encoding.UTF8);
char[] read = new char[MaxReadCharacters];
int count = reader.Read(read, 0, MaxReadCharacters);
while (count > 0)
{
sb.Append(read, 0, count);
count = reader.Read(read, 0, MaxReadCharacters);
}
MemoryStream mStream = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(sb.ToString()));
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(BingDataContractResponse));
object objResponse = jsonSerializer.ReadObject(mStream);
BingDataContractResponse jsonResponse = objResponse as BingDataContractResponse;
// Display the status.
//// Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = resStream;
// Open the stream using a StreamReader for easy access.
StreamReader reader2 = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader2.ReadToEnd();
// Display the content.
//// Console.WriteLine(responseFromServer);
// Clean up the streams and the response.
Location location = (Location)jsonResponse.ResourceSets[0].Resources[0];
//// if (location.Confidence == "High")
//// {
//// Console.WriteLine(location.Address.FormattedAddress);
//// }
Console.WriteLine();
reader.Close();
response.Close();
resStream.Close();
}
} // end of class
} // end of namespace