示例#1
0
		HttpWebRequest Send(BaseRequest f)
		{
			if (f == null)
				throw new ArgumentNullException("f");

			HttpWebRequest request;
			bool hasBody = false;
			switch (f.RequestVerb)
			{
				case "GET":
				case "DELETE":
				case "HEAD":
					{
						var u = string.Format("{0}json={1}&", f.RequestUri, HttpUtility.UrlEncode(f.RequestBody));
						request = WebRequest.Create(u) as HttpWebRequest;
					}
					break;
				case "POST":
				case "PUT":
				default:
					{
						request = WebRequest.Create(f.RequestUri) as HttpWebRequest;
						hasBody = !string.IsNullOrWhiteSpace(f.RequestBody);
					}
					break;
			}
			request.Method = f.RequestVerb;
			request.ContentType = "application/" + f.InputFormat;
			request.Expect = "application/" + f.OutputFormat;

			if (hasBody)
			{
				byte[] buffer = Encoding.UTF8.GetBytes(f.RequestBody);
				request.ContentLength = buffer.Length;
				using (Stream rs = request.GetRequestStream())
				{
					rs.Write(buffer, 0, buffer.Length);
					rs.Flush();
					rs.Close();
				}
			}
			return request;
		}
示例#2
0
		public MapQuestResponse Execute(BaseRequest f)
		{
			HttpWebRequest request = Send(f);
			MapQuestResponse r = Parse(request);
			if (r != null && !r.Results.IsNullOrEmpty())
			{
				foreach (MapQuestResult o in r.Results)
				{
					if (o == null)
						continue;

					foreach (MapQuestLocation l in o.Locations)
					{
						if (!string.IsNullOrWhiteSpace(l.FormattedAddress) || o.ProvidedLocation == null)
							continue;

						if (string.Compare(o.ProvidedLocation.FormattedAddress, "unknown", true) != 0)
							l.FormattedAddress = o.ProvidedLocation.FormattedAddress;
						else
							l.FormattedAddress = o.ProvidedLocation.ToString();
					}
				}
			}
			return r;
		}