Httpclient代码
/// <summary>
/// 显示
/// </summary>
/// <returns></returns>
public ActionResult get()
{
Uri url = new Uri("http://localhost:49903/");
HttpClient client = new HttpClient();
client.BaseAddress = url;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/jaon"));
HttpResponseMessage message = client.GetAsync("api/Product").Result;
List<day19info> list = new List<day19info>();
if (message.IsSuccessStatusCode)
{
list = message.Content.ReadAsAsync<List<day19info>>().Result;
}
else
{
ViewBag.mess = "请求错误!";
}
return View(list);
}
/// <summary>
/// 新增
/// </summary>
/// <returns></returns>
public ActionResult ADD()
{
return View();
}
[HttpPost]
public ActionResult ADD(day19info s)
{
Uri url = new Uri("http://localhost:49903/");
HttpClient client = new HttpClient();
client.BaseAddress = url;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpContent httpcontent = new StringContent("{'name':\"" + s.name + "\",'num':\"" + s.num + "\",'prices':\"" + s.prices + "\"}");
httpcontent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
HttpResponseMessage message = client.PostAsync("api/Product",httpcontent).Result;
if (message.IsSuccessStatusCode)
{
ViewBag.mess = message.Content.ReadAsStringAsync().Result;
}
else
{
ViewBag.mess = "请求错误!";
}
return Content("<script>alert('添加成功!');location.href='/Home/get'</script>");
}
/// <summary>
/// 修改
/// </summary>
/// <returns></returns>
public ActionResult upt()
{
return View();
}
[HttpPost]
public ActionResult upt(int id,day19info s)
{
Uri url = new Uri("http://localhost:49903/");
HttpClient client = new HttpClient();
client.BaseAddress = url;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpContent httpcontent = new StringContent("{'id':'"+id+"','name':\""+s.name+"\",'num':'"+s.num+"','prices':\""+s.prices+"\" }");
httpcontent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
HttpResponseMessage message = client.PutAsync("api/Product/" + id, httpcontent).Result;
if (message.IsSuccessStatusCode)
{
ViewBag.mess = message.Content.ReadAsStringAsync().Result;
}
else
{
ViewBag.mess = "请求失败!";
}
return Content("<script>alert('修改成功!');location.href='/Home/get'</script>");
}
/// <summary>
/// 删除
/// </summary>
/// <returns></returns>
public ActionResult del(int id)
{
Uri url = new Uri("http://localhost:49903/");
HttpClient client = new HttpClient();
client.BaseAddress = url;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage message = client.DeleteAsync("api/Product/" + id).Result;
if (message.IsSuccessStatusCode)
{
ViewBag.mess = message.Content.ReadAsStringAsync().Result;
}
else
{
ViewBag.mess = "失败";
}
return Content("<script>alert('删除成功!');location.href='/Home/get'</script>");
}
function chaxun()
{
var name = $("#name").val();
$.ajax({
url: "/chaApi/Product/cha/" + name,
type: "Get",
success: function (data) {
$("#body").empty();
var str = "";
for (var i = 0; i < data.length; i++) {
str += " <tr>";
str += "<td>" + data[i].id + "</td>";
str += "<td>" + data[i].name + "</td>";
str += "<td>" + data[i].num + "</td>";
str += "<td>" + data[i].prices + "</td>";
str += "<td><input type=\"button\" name=\"name\" value=\"del \" onclick=\"del("+data[i].id+")\" /><input type=\"button\" name=\"name\" value=\"Upt \" onclick=\"upt("+data[i].id+")\" /></td>";
str += " </tr>";
}
$("#body").append(str);
}
})
}
Httpclient代码的更多相关文章
- [转][C#]HttpClient 代码示例
转自:https://www.cnblogs.com/amosli/p/3918538.html 也参考了:https://www.cnblogs.com/ShadowFiend007/p/80668 ...
- HttpClient代码设置代理
由于对接faceBook接口,本地测试时候要设置代理才能调试. (http和https通用) public SSLContext createIgnoreVerifySSL() throws NoSu ...
- ASP.NET CORE 2.* 利用集成测试框架覆盖HttpClient相关代码
ASP.NET CORE 集成测试官方介绍 我的asp.net core 项目里面大部分功能都是去调用别人的API ,大量使用HttpClient,公司单元测试覆盖率要求95%以上,很难做到不mock ...
- HttpClient实现客户端与服务器的通信
本篇主要讲解了利用HttpClient实现 windows主机与linux服务器的通信与传递数据 HttpClient代码,服务器端配置 系统和安装软件 1)ubuntu 14.04 64位系统 2) ...
- Android入门:用HttpClient模拟HTTP的GET和POST请求
一.HttpClient介绍 HttpClient是用来模拟HTTP请求的,其实实质就是把HTTP请求模拟后发给Web服务器: Android已经集成了HttpClient,因此可以直接使用: ...
- (干货)一次httpclient的close_wait问题的探讨
从图中可以看出,如果客户端被动关闭连接,且没有向服务器端发送FIN,则会一直处于CLOSE_WAIT状态. 处理服务器在处理完请求,与后端Nginx之间的连接仍然保持着CLOSE_WAIT状态,个数为 ...
- HttpClient笔记与踩过的坑
本来有个指纹采集功能做了个winFrom小程序 在本地测试都还能行,后来快上线的时候发现 客户用的阿里云数据库, 不对外公布 ,然后发现本地采集的数据没办法上传到数据库怎么办呢? 然后曲线救国,用we ...
- C#5.0异步编程 HttpClient IP代理验证原码
//访问HttpClient 代码 public async Task<string> VerifyProxy(string url, string proxy = "" ...
- 【转】 Pro Android学习笔记(七一):HTTP服务(5):多线程调用HttpClient
目录(?)[-] 应用共享HttpClient对象的同步问题 创建共享HttpClient代码 创建共享对象 创建可共享的HttpClient对象 使用共享HttpClient对象的代码 基础代码 修 ...
随机推荐
- go语言实现生产者-消费者
前言: 之前在学习操作系统的时候,就知道生产者-消费者,但是概念是模模糊糊的,好像是一直没搞明白. 其实很简单嘛,生产者生产,消费者进行消费,就是如此简单.了解了一下go语言的goroute,感觉实现 ...
- Laravel-google-authenticator--Google验证码
开发前的准备 安装Laravel 安装二维码生成器QrCode,没有安装也可以,接下来会安装 安装拓展 1.运行如下代码安装拓展包: composer require "earnp/lara ...
- form表单中button按钮返回上一页解决办法
解决Form中button按钮不好用的问题解决办法. 方法一: 1.在Form表单标签中国增加一个属性,如下图,红框处 2.返回按钮样式 3.onclick方法需要跳转的页面,遮挡处为需要返回的页面 ...
- NOIP2018Day1T1 铺设道路
题目描述 春春是一名道路工程师,负责铺设一条长度为 \(n\) 的道路. 铺设道路的主要工作是填平下陷的地表.整段道路可以看作是 \(n\) 块首尾相连的区域,一开始,第 \(i\) 块区域下陷的深度 ...
- mui slider禁止滑动
网上方法: mui('.mui-slider').slider().setStopped(true); 实际使用 mui('.mui-slider').slider().stopped = true; ...
- Vue过滤器使用
格式(一个过滤器):{{ 'msg' | filterA }} (多个过滤器):{{ 'msg' | filterA | filterB }} window.onload =function(){ / ...
- windows安装多个python及pip版本
windows安装多个python及pip版本 1.下载所需要的python2和python3安装包 2.一路next 3.设置环境变量 4.修改python安装目录下的可执行程序名称 5.在cmd中 ...
- leetcode python快乐数
编写一个算法来判断一个数是不是“快乐数” “快乐数”的定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复该过程直到为1,也可能是无限循环但始终变不到1. 如果可以变为1,那 ...
- 自定义Section
转载 :http://www.cnblogs.com/gaobing/p/6047746.html <configSections> 元素必须是 configuration 元素的第一个子 ...
- Spring Cloud 快速入门
Spring Cloud快速入门 代码地址: https://gitee.com/gloryxu/spring-cloud-test EureKa:服务注册中心 添加依赖 <dependenc ...