public class RequestHelper
{
/// <summary>
/// 发起post请求
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="url">url</param>
/// <param name="postData">post数据</param>
/// <returns></returns>
public static T PostResponse<T>(string url, object postData)
{
string json = JsonHelper.ToJson(postData);
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpContent httpContent = new StringContent(json);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpClient httpClient = new HttpClient();

T result = default(T);

HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;

if (response.IsSuccessStatusCode)
{
Task<string> t = response.Content.ReadAsStringAsync();
string s = t.Result;
result = JsonHelper.DeSerializeObject<T>(s);
}
return result;
}

/// <summary>
/// 发起get请求
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="url">url</param>
/// <returns></returns>
public static T GetResponse<T>(string url)
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpClient httpClient = new HttpClient();
T result = default(T);
HttpResponseMessage response = httpClient.GetAsync(url).Result;

if (response.IsSuccessStatusCode)
{
Task<string> t = response.Content.ReadAsStringAsync();
string s = t.Result;
result = JsonHelper.DeSerializeObject<T>(s);
}
return result;
}
}

调用:

List<double[]> logAndLat = new List<double[]>();

logAndLat.Add(new double[] {113.326196, 34.715269 });

logAndLat.Add(new double[] {113.321561, 34.722183});

public static double GetArea(List<double[]> points)
{
string result = RequestHelper.PostResponse<string>("url", points);
double.TryParse(result, out double r);
return r;
}

最新文章

  1. Error:Excepted resource of type id
  2. c++中处理输入输出的方法
  3. IE浏览器下常见的CSS兼容问题
  4. 分享——张南《从Desktop到Mobile的自动化测试实践》
  5. Android实现网络音乐播放器
  6. Python练习题 025:判断回文数
  7. SharePoint 101 Code Samples are now available
  8. 转载MSDN 在ASP.NET 中执行 URL 重写
  9. 如何改变word修订模型下的视图
  10. CentOS和Redhat发行版linux内核版本的对应关系
  11. 考试easy该,学习如何做?
  12. Swift下自定义xib添加到Storyboard
  13. Haskell学习-functor
  14. 从架构演进的角度聊聊Spring Cloud都做了些什么
  15. solus 系统 - 编译安裝 ibus-rime 中文输入法(附:小鹤双拼双形配置文件)
  16. 三、用Delphi10.3 创建一条JSON数据的第三种方法,非常简洁的写法
  17. python图片处理(一)
  18. 用java查询HBase中某表的一批数据
  19. c/c++ json使用
  20. jquery数组拼接

热门文章

  1. Could not instantiate bean class [org.springframework.data.mongodb.core.MongoTemplate]
  2. 11. 变量提升 &amp;&amp; 执行上下文
  3. appium中driver.wait报IllegalMonitorStateException的解释
  4. Number Sequence (KMP第一次出现位置)
  5. C#工具类之字符串扩展类
  6. 需要了解的几个Java基础点
  7. python3+selenium获取列表某一列的值
  8. 3.Servlet(二)
  9. 移动测试之appium+python 环境安装(一)
  10. Android Studio CMake依赖第三方库