C # Analog HTTP sends a post or GET request

Source: Internet
Author: User

<summary>
Get city names based on latitude and longitude
</summary>
<param name= "lat" > Latitude </param>
<param name= "LNG" > Longitude </param>
<returns> City Name </returns>
public static string Getcity (String lat, string lng)
{
String url = string. Format (
"{0}?output={1}&ak={2}&location={3},{4}",
configurationmanager.appsettings["Baidugeocodingapi"],
"JSON",
configurationmanager.appsettings["Baiduak"],
Lat
LNG);

String citystring = Remotequery.getresponse (URL);
if (string. IsNullOrEmpty (citystring)) return string. Empty;

Jobject city = (jobject) jsonconvert.deserializeobject (citystring);

if (city["status"). ToString ()! = "0")
{
Log. Error ("Get City error with Status:" + city["status"]);
return string. Empty;
}

Return (string) city["Result" ["addresscomponent"] ["City"];
}

public static string GetResponse (String accessurl)
{
string response = string. Empty;
Try
{
#region access the service URL and get the JSON data returned
HttpWebRequest request = (HttpWebRequest) httpwebrequest.create (Accessurl);
Request. Method = "GET";
Request. ContentType = "application/x-www-form-urlencoded";
Request. Credentials = CredentialCache.DefaultCredentials;
Request. Timeout = 10000;
HttpWebResponse HttpWebResponse = null;
HttpWebResponse = Request. GetResponse () as HttpWebResponse;
StreamReader sr = new StreamReader (Httpwebresponse.getresponsestream (), Encoding.UTF8);
Response = Sr. ReadToEnd ();
Sr. Close ();
#endregion
}
catch (Exception ex)
{
Log. Info ("Error when accessing:" + Accessurl);
Log. Error (ex. Message);
}
return response;
}
}

<!--configuration file Weather longitude=116.31985&latitude=39.959836, Beijing--
<add key= "Baiduweatherapi" value= "Http://api.map.baidu.com/telematics/v3/weather"/>
<add key= "Baiduak" value= "3lx7gmqlza5kzqwde4zizmul"/>

C # Analog HTTP sends a post or GET request

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.