.net 控制器调用外部链接传参方法
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;
}
最新文章
- Error:Excepted resource of type id
- c++中处理输入输出的方法
- IE浏览器下常见的CSS兼容问题
- 分享——张南《从Desktop到Mobile的自动化测试实践》
- Android实现网络音乐播放器
- Python练习题 025:判断回文数
- SharePoint 101 Code Samples are now available
- 转载MSDN 在ASP.NET 中执行 URL 重写
- 如何改变word修订模型下的视图
- CentOS和Redhat发行版linux内核版本的对应关系
- 考试easy该,学习如何做?
- Swift下自定义xib添加到Storyboard
- Haskell学习-functor
- 从架构演进的角度聊聊Spring Cloud都做了些什么
- solus 系统 - 编译安裝 ibus-rime 中文输入法(附:小鹤双拼双形配置文件)
- 三、用Delphi10.3 创建一条JSON数据的第三种方法,非常简洁的写法
- python图片处理(一)
- 用java查询HBase中某表的一批数据
- c/c++ json使用
- jquery数组拼接
热门文章
- Could not instantiate bean class [org.springframework.data.mongodb.core.MongoTemplate]
- 11. 变量提升 &;&; 执行上下文
- appium中driver.wait报IllegalMonitorStateException的解释
- Number Sequence (KMP第一次出现位置)
- C#工具类之字符串扩展类
- 需要了解的几个Java基础点
- python3+selenium获取列表某一列的值
- 3.Servlet(二)
- 移动测试之appium+python 环境安装(一)
- Android Studio CMake依赖第三方库