Content-Type boundary 问题
我并不知道问题怎么描述清楚一些。
事情是这样的,使用 Microsoft Dynamics CRM Server 2016 做CRM系统的时候用到 使用 Web API 执行批处理操作(参见SDK或者 https://msdn.microsoft.com/zh-cn/mt607719 ).
官方只给出 相应报文的 文本。告诉你 响应头 怎么写,相应体 怎么写,而我要做的是用c# 实现它。
首先 我使用了 谷歌浏览器 的一个插件,叫 postmen ,第一次听同学说起,还下载错了,下载了 成了 桌面应用程序,其实 是插件。
为啥我用插件呢,因为网站需要登录才能调API啊。需要共享秘钥。

就长这样哈。
然后照着官网的例子,改改就测试通过了。
接下来 就要写 c#代码了。
然后遇到问题了。
不知道怎么 生成Content-Type: multipart/mixed;boundary=changeset_BBB456 !!!
首先尝试了HttpClient 类, DefaultRequestHeaders.TryAddWithoutValidation 方法试了试,不行,位置不对。
StringContent 类试了试 也不行。
最后发现 了 MultipartContent类,果断可以了。
代码如下哈,因为里面有项目封装的一些东西,copy过去 是不能用的哈,意思就这意思。
public static ApiResult PostBatch()
{
object obj = new object();
string url = "$batch"; var batchUniqueIdHeader = "batch_AAA123";//Guid.NewGuid().ToString(); ICredentials credentials = null; EnsureCredentials(ref credentials);
using (var client = new CRMClient(credentials, null))
{
var data = new
{
neo_phonesysstatus =
};
var multipartContent = new MultipartContent("mixed", "batch_AAA123");
var stringContent = new StringContent(string.Format(@"
--{0}
Content-Type: application/http
Content-Transfer-Encoding:binary
Content-ID: 1 PATCH http://dev.crm.xiaoniu66.com/DevCRM/api/data/v8.0/neo_marketings(B1D14BC0-2B6B-E611-80C4-005056A97E8D) HTTP/1.1
Content-Type: application/json;type=entry {1} --{0}--
", "changeset_BBB456", data.ToJson()));
stringContent.Headers.Clear();
stringContent.Headers.Add("Content-Type", "multipart/mixed;boundary=batch_AAA123");
multipartContent.Add(stringContent);
HttpResponseMessage response = client.PostAsync(url, multipartContent).Result;
var result = HandleApiResult(response);
return result;
}
}
其中 遇到的几个问题是,要记住 响应头 必须 和 相应体 中间隔着空行。
Content-Type boundary 问题的更多相关文章
- org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data;boundary=----WebKitFormBoundaryRAYPKeHKTYSNdzc1;charset=UTF-8' not supported
原文:https://www.cnblogs.com/yueli/p/7552888.html 最近同事在做一个图片上传功能.在入参 body 中同时传入文件和其它基本信息结果出现如题异常.在此记录下 ...
- the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header
the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header ...
- Jsoup问题---获取http协议请求失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.
Jsoup问题---获取http协议请求失败 1.问题:用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不 ...
- Jsoup获取部分页面数据失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.
用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不符合要求. 请求代码如下: private static ...
- SharePoint自动化系列——Add content type to list.
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 将创建好的content type(若是跨web application需要事先publish c ...
- SharePoint自动化系列——Content Type相关timer jobs一键执行
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 背景: 在SharePoint Central Administration->Monito ...
- 转载 SharePoint【Site Definition 系列】– 创建Content Type
转载原地址: http://www.cnblogs.com/wsdj-ITtech/archive/2012/09/01/2470274.html Sharepoint本身就是一个丰富的大容器,里面 ...
- Springs Element 'beans' cannot have character [children], because the type's content type is element-only
Springs Element 'beans' cannot have character [children], because the type's content type is element ...
- springboot 报错 Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
开始 controller 方法写的是 @RequestMapping( value = "/add", method = RequestMethod.POST ) public ...
- .NET获取文件的MIME类型(Content Type)
第一种:这种获取MIME类型(Content Type)的方法需要在.NET 4.5之后才能够支持,但是非常简单. 优点:方便快捷 缺点:只能在.NET 4.5之后使用 public FileResu ...
随机推荐
- Block学习一门:基本使用,使用block包NSURLRequest异步请求
首先,看一下下面的代码: void (^myFirstBlock)(int theOne,int theTwo) = ^(int theOne,int theTwo){ NSLog(@"== ...
- SDUT 2894-C(最短spfa)
C Time Limit: 7000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 给出一个带权无向图.包括n个点,m条边.求出s,e的最短路.保证最短路存在 ...
- Storm基础知识
上一篇文章我们介绍一个简单的Storm起源,今天我去学习Storm一些主要的知识,他的基本使用基本的了解.幸运的是,,不是太困难,假设我们理解Hadoop的MapReduce模型的话.看这个也是很类似 ...
- ORA-01791: not a SELECTed expression 一种是不 bug 的 bug!
[ora11@lixora ~]$ !sql sqlplus / as sysdba SQL*Plus: Release 11.2.0.1.0 Production on Wed Aug 27 09: ...
- structs2注解+jsp+ajax实现post异步载入select
流程: 1.点击载入btn发起异步请求post 2.后台处理请求返回数据 3.前端获取数据成功,对数据进行处理 前端: html:首先要导入jq包,不然怎么用ajax呢. <script typ ...
- ehCache浅谈(转)
ehcache FAQ中提到 Remember that a value in a cache element is globally accessible from multiple threads ...
- java major version(转)
在jar包中,用winrar解压一个类文件,然后在命令行下面输入 javap -verbose classname 会输出一些信息,大致如下: Compiled from "HtmlCraw ...
- C++ - 派生类访问模板基类(templatized base class)命名
派生类访问模板基类(templatized base class)命名 本文地址: http://blog.csdn.net/caroline_wendy/article/details/239936 ...
- teamfoundationserver2010之使用vs2010&&vs2013创建修改签入签出提交代码等操作
好久之前就在个人的电脑上装了vs2013,看到原生支持html5和css3,前端开发还是挺不错的 而公司开发一直用着vs2010,真是羡慕嫉妒恨啊... 于是乎就有了今天的测试 使用工具 teamfo ...
- [6] 算法路 - 双向冒泡排序的Shaker
Shaker序列 –算法 1. 气泡排序的双向进行,先让气泡排序由左向右进行.再来让气泡排序由右往左进行,如此完毕一次排序的动作 2. 使用left与right两个旗标来记录左右两端已排序的元素位置. ...