httppost请求.

applicationkey/x-www-form-urlencoded

请求: Email=321a&Name=kkfew

webapi里面, 如果用实体, 能接受到. 因为这是一个属性一个属性去匹配的原因
application/json

请求:{Email:321a  ,  Name:kkfew}

如果接收参数为dynamic, 则没有问题.
因为直接把json对象转化成dynamic对象了

using (WebClient webclient = new WebClient())
{ string apiurl = "http://192.168.1.225:9090/api/v3productsapi/GetStatisticInfo"; webclient.Encoding = UTF8Encoding.UTF8; string auth_key = string.Format("{0}:{1}:{2}", ts.TotalSeconds, randomStr, token);
webclient.Headers.Add("Custom-Auth-Key", auth_key); Console.Write(webclient.DownloadString(apiurl));
} using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://192.168.1.225:9090/");
client.DefaultRequestHeaders.Add("aa", "11");
var result = client.GetStringAsync("/api/v3productsapi/GetStatisticInfo").Result;
Console.WriteLine(result);
}

post

System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();

            using (var client = new HttpClient())//httpclient的post方式, 需要被实体接收..
{
string apiurl = "http://192.168.1.225:9090/api/V3ImageUploadApi/posttestform2";
//3个枚举, stringContent,ByteArrayContent,和FormUrlContent, 一般的post用StringContent就可以了
using (var content = new StringContent(
"Email=321a&Name=kkfew", Encoding.UTF8, "application/x-www-form-urlencoded"))
{
content.Headers.Add("aa", "11"); //默认会使用
var result = client.PostAsync(apiurl, content).Result;
Console.WriteLine(result);
}
}
using (var client = new HttpClient())
{
string apiurl = "http://192.168.1.225:9090/api/V3ImageUploadApi/posttestform";
using (var content = new StringContent(json.Serialize(new { Email = "1", Name = "2" })))
{
content.Headers.Add("aa", "11");
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var result = client.PostAsync(apiurl, content).Result;
Console.WriteLine(result);
}
}
using (WebClient webclient = new WebClient())
{ string apiurl = "http://192.168.1.225:9090/api/V3ImageUploadApi/posttestform2"; webclient.Encoding = UTF8Encoding.UTF8;
webclient.Headers.Add("Custom-Auth-Key", "11");
webclient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//x-www-form-urlencoded
//如果webapi的接收参数对象是dynamic, 则请求的头是json, 如果是用实体接收, 那么则是上面这个 var re = webclient.UploadData(apiurl,
System.Text.Encoding.UTF8.GetBytes("Email=321a&Name=kkfew")); Console.Write(System.Text.Encoding.UTF8.GetString(re));
} using (WebClient webclient = new WebClient())
{ string apiurl = "http://192.168.1.225:9090/api/V3ImageUploadApi/posttestform"; webclient.Encoding = UTF8Encoding.UTF8;
webclient.Headers.Add("Custom-Auth-Key", "11");
webclient.Headers.Add("Content-Type", "application/json");//x-www-form-urlencoded
//如果webapi的接收参数对象是dynamic, 则请求的头是json, 如果是用实体接收, 那么则是上面这个 var re = webclient.UploadData(apiurl,
System.Text.Encoding.UTF8.GetBytes(json.Serialize(new { email = "123456@qq.com", password = "111111" }))); Console.Write(System.Text.Encoding.UTF8.GetString(re));
}

上传图片

using (WebClient webclient = new WebClient())
{ string apiurl = "http://192.168.1.225:9090/api/V3ImageUploadApi/post"; webclient.Encoding = UTF8Encoding.UTF8;
string auth_key = string.Format("aa", "111");
webclient.Headers.Add("Custom-Auth-Key", auth_key); byte[] b = webclient.UploadFile(apiurl, @"c:\2.jpg"); Console.Write(System.Text.Encoding.UTF8.GetString(b));
} using (var client = new HttpClient())
{
using (var content = new MultipartFormDataContent())
{
client.BaseAddress = new Uri("http://192.168.1.225:9090/");
var fileContent = new ByteArrayContent(File.ReadAllBytes(@"c:\1.jpg"));
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "1.jpg"
}; content.Add(fileContent);
content.Headers.Add("aa", "ssssss");
var result = client.PostAsync("/api/V3ImageUploadApi/post", content).Result;
Console.WriteLine(result.Content.ReadAsStringAsync().Result);
}
}
[HttpPost]
public HttpResponseMessage Post()
{
try
{
if (!IsValidateTokenPost)
{
return TokenException();
} if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
var apiResult = new ApiResult(); string root = HostingEnvironment.MapPath("~/content/images/uploaded/fromwechart");
if (!System.IO.Directory.Exists(root))
System.IO.Directory.CreateDirectory(root);
var provider = new MultipartFormDataStreamProvider(root);
string OssFilename = ""; //下面代码让异步变成同步, 因为要返回上传完图片之后新的图片绝对路径
IEnumerable parts = null;
Task.Factory
.StartNew(() =>
{
parts = Request.Content.ReadAsMultipartAsync(provider).Result.Contents;
foreach (MultipartFileData file in provider.FileData)
{
//Trace.WriteLine(file.Headers.ContentDisposition.FileName);
//Trace.WriteLine("Server file path: " + file.LocalFileName);
var fileName = file.Headers.ContentDisposition.FileName;
OssFilename = System.Guid.NewGuid() + "." + fileName.Split('.')[1];
using (FileStream fs = new FileStream(file.LocalFileName, FileMode.Open))
{
PutImageFileToOss(fs, "image/" + fileName.Split('.')[1], OssFilename); }
if (File.Exists(file.LocalFileName))//上传完删除
File.Delete(file.LocalFileName);
}
},
CancellationToken.None,
TaskCreationOptions.LongRunning, // guarantees separate thread
TaskScheduler.Default)
.Wait(); string picUrl = "http://" + OssService.OssConfig.Domain + "/" + "content/sitefiles/" + _StoreContext.CurrentStore.SiteId + "/images/" + OssFilename; return Json(new ApiResult { StatusCode = StatusCode.成功, Info = new { filename = picUrl } });
}
catch (Exception ex)
{
return Json(ApiResult.InnerException(ex));
} }

WebClient和HttpClient, 以及webapi上传图片的更多相关文章

  1. 使用HttpClient调用WebAPI接口,含WebAPI端示例

    API端: using log4net; using System; using System.Collections.Generic; using System.IO; using System.L ...

  2. WebApi上传图片 await关键字

    await关键字对于方法执行的影响 将上一篇WebApi上传图片中代码修改(使用了await关键字)如下: [HttpPost] public async Task<string> Pos ...

  3. kindeditor修改图片上传路径-使用webapi上传图片到图片服务器

    kindeditor是一个非常好用的富文本编辑器,它的简单使用我就不再介绍了. 在这里我着重介绍一些使用kindeditor修改图片上传路径并通过webapi上传图片到图片服务器的方案. 因为我使用的 ...

  4. kindeditor扩展粘贴图片功能&修改图片上传路径并通过webapi上传图片到图片服务器

    前言 kindeditor是一个非常好用的富文本编辑器,它的简单使用我就不再介绍了. 而kindeditor却对图片的处理不够理想. 本篇博文需要解决的问题有两个: kindeditor扩展粘贴图片功 ...

  5. kindeditor扩展粘贴截图功能&修改图片上传路径并通过webapi上传图片到图片服务器

    前言 kindeditor是一个非常好用的富文本编辑器,它的简单使用我就不再介绍了. 而kindeditor却对图片的处理不够理想. 本篇博文需要解决的问题有两个: kindeditor扩展粘贴图片功 ...

  6. HttpRequest,WebRequest,HttpWebRequest,WebClient,HttpClient 之间的区别

    HttpRequest,WebRequest,HttpWebRequest,WebClient,HttpClient 今天我们来聊一下他们之间的关系与区别. HttpRequest 类 .NET Fr ...

  7. Asp.Net WebApi上传图片

    webapi using System; using System.Collections; using System.Collections.Generic; using System.Diagno ...

  8. webrequest、httpwebrequest、webclient、HttpClient 四个类的区别

    一.在 framework 开发环境下: webrequest.httpwebreques  都是基于Windows Api 进行包装, webclient 是基于webrequest 进行包装:(经 ...

  9. .Net5下WebRequest、WebClient、HttpClient是否还存在使用争议?

    WebRequest.WebClient.HttpClient 是C#中常用的三个Http请求的类,时不时也会有人发表对这三个类使用场景的总结,本人是HttpClient 一把梭,也没太关注它们的内部 ...

随机推荐

  1. 解决ubuntu sudo not found command的问题

    将/etc/sudoers 中Defaults env_reset改成Defaults !env_reset取消掉对PATH变量的重置, 然后在/etc/bash.bashrc中最后添加alias s ...

  2. QQ一键登录功能的实现过程

    QQ登录的思路: 当qq登陆成功后,QQ会给我们返回一个唯一的用户标识:openId,当用户授权QQ时,判断 if(已经有openId){ 跳转到登陆后的页面. }else if(没有openId){ ...

  3. 迅为4412开发板Linux驱动教程——总线_设备_驱动注册流程详解

    本文转自:http://www.topeetboard.com 视频下载地址: 驱动注册:http://pan.baidu.com/s/1i34HcDB 设备注册:http://pan.baidu.c ...

  4. 开创学习的四核时代-迅为iTOP4412学习开发板

    产品特点: 处理器: Exynos 4412 处理器,Cortex-A9四核,功耗性能俱佳! 性能: 1GB(可选2GB) 双通道 64bit数据总线 DDR3: 4GB(可选16GB)固态硬盘EMM ...

  5. CCDH证书

    4月份有些冲动,想报名考个CCDH证书,一直没有找到合适的付款方式,因为自己没有外币信用卡, 后来受到朋友的帮助,22号付了款,26号就去考了试,不是很满意,如果少冲动一下,多看两天书, 效果会更好.

  6. Windows事件ID大全

    51 Windows 无法找到网络路径.请确认网络路径正确并且目标计算机不忙或已关闭.如果 Windows 仍然无法找到网络路径,请与网络管理员联系. 52 由于网络上有重名,没有连接.请到“控制面板 ...

  7. IT技术团队管理-总结

    摘要:此文是书籍<行之有效:IT技术团队管理之道>的读书笔记. 主要是方便自己回顾. 您也可以通过此文简要了解此书的内容. 博客: http://www.cnblogs.com/jhzhu ...

  8. javascript/jquery键盘事件介绍

    一.首先需要知道的是:1.keydown()keydown事件会在键盘按下时触发.2.keyup()keyup事件会在按键释放时触发,也就是你按下键盘起来后的事件3.keypress()keypres ...

  9. SGU 180 Inversions

    题意:求逆序数对数量. 思路一:暴力,O(N^2),超时. 思路二:虽然Ai很大,但是n比较小,可以离散化,得到每个Ai排序后的位置Wi,然后按照输入的顺序,每个Ai对答案的贡献是Wi-Sum(Wi- ...

  10. Java的文件读写操作 <转>

    目录: file内存----输入流----程序----输出流----file内存 java中多种方式读文件 判断文件是否存在不存在创建文件 判断文件夹是否存在不存在创建文件夹 java 写文件的三种方 ...