xamarin.forms - DataServiceContext.ExecuteAsync 导致有关 User-Agent 标头的异常
问题描述
我创建了一个 Xamarin.Forms 应用程序并添加了一个连接的 Odata 服务。现在,当我尝试向后端发送请求时,它将失败并出现异常 System.ArgumentException: The 'User-Agent' header must be modified using the appropriate property or method。参数名称:名称。
我试过的:
1:处理 DataServiceContext_BuildingRequest 并使用 e.Headers["User-Agent"] = "xyz";
2:设置 Content-Type 标头,因为我在另一个线程中读到它有帮助的地方(e.Headers.Add("Content-Type", "application/json"))
3:设置DataServiceContext.Configurations.RequestPipeline.OnMessageCreating的委托。问题是,一旦我尝试创建一个新的 HttpWebRequestMessage 实例并传入它接收到的参数,就会抛出异常。
管道代表:
Configurations.RequestPipeline.OnMessageCreating = (args) =>
{
args.Headers["User-Agent"] = "xyz";
args.Headers.Add("Content-Type", "application/json");
args.Headers["Accept"] = "application/json";
var req = new HttpWebRequestMessage(args);
req.HttpWebRequest.ContentType = "application/json";
return req;
};
我对此的怀疑是,Odata HttpWebRequestMessage 类使用的底层代码发生了一些变化,不再允许直接设置 User-Agent 标头。
有没有人找到解决这个问题的方法?
解决方案
推荐阅读
- python - 将多个数据帧中的列转换为布尔值
- c++ - gluLookAt 问题 - 如何使曲线运动?
- python - 如何从 SVG 裁剪空白空间?
- python - 使用键过滤嵌套字典并获取字典中的项目
- windows - psexec 不工作,即使它说它可以工作
- apache2 - 只读服务器中的 Apache2 WebDAV 可写目录
- vb.net - 如何将文本从文本文件传输到在某个字符上拆分的列表框?
- facebook - Facebook OAuth 安全使用护照-facebook
- google-sheets - 根据另一个单元格值更改单元格的颜色
- python - 我不明白我的列表索引如何超出范围