我正在使用C# SDK,似乎在执行任何POST时都有错误。post正文被序列化为字符串"System.IO.StringReader“。这似乎是因为内部/Http/DefaultHttpClient.cs中的第127行
我将代码更改为:
restRequest.AddParameter("application/json", smartsheetRequest.Entity.GetContent(),ParameterType.RequestBody);
至:
restRequest.AddParameter("application/json", smartsheetRequest.Entity.GetContent().ReadToEnd(),ParameterType.RequestBody);
它似乎解决了这个问题。能请Smartsheet公司的人确认一下吗?
Thx
发布于 2014-04-17 15:04:51
谢谢你让我们注意到这一点!我们能够在今天推出的一个版本中包含这方面的修复。只需升级您的包使用NuGet或下载从最新源代码的最新源代码。
此版本的更改列表如下:
技术细节
这个问题是在我们发布版本1.0.1的前两天3月17日引入的,具体原因是为了支持二进制附件而删除了ReadToEnd()方法的提交ReadToEnd。
这个问题是由您指出的smartsheetRequest.Entity.GetContent()
允许restRequest对象调用ToString()的行引起的,它给了我们一个请求主体,如下所示:
POST https://api.smartsheet.com/1.1/home/folders HTTP/1.1
Authorization: Bearer THE_TOKEN
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
User-Agent: smartsheet-csharp-sdk(sdk-csharp-sample)/0.0.0.1 Microsoft Windows 7 Enterprise
Content-Type: application/json
Host: api.smartsheet.com
Content-Length: 22
Accept-Encoding: gzip, deflate
System.IO.StreamReader
最后一行应该是StreamReader的真实内容,而不是StreamReader的ToString()。
您提到的在ReadToEnd()上使用StreamReader的解决方案是一个很好的解决方案,只是它不处理二进制数据。我们使用GetBinaryContent()
方法实现了这个更改。然后,我们使用Util.ReadAllBytes(...)
将其转换为字节数组。
确切的更改以diff格式列出如下:
diff --git a/main/Smartsheet/Api/Internal/Http/DefaultHttpClient.cs b/main/Smartsheet/Api/Internal/Http/DefaultHttpClient.cs
index 5913935..df6d7d5 100644
--- a/main/Smartsheet/Api/Internal/Http/DefaultHttpClient.cs
+++ b/main/Smartsheet/Api/Internal/Http/DefaultHttpClient.cs
@@ -121,10 +121,10 @@ namespace Smartsheet.Api.Internal.Http
restRequest.AddHeader(header.Key, header.Value);
}
}
-
+
if (smartsheetRequest.Entity != null && smartsheetRequest.Entity.GetContent() != null)
{
- restRequest.AddParameter("application/json", smartsheetRequest.Entity.GetContent(),
+ restRequest.AddParameter("application/json", Util.ReadAllBytes(smartsheetRequest.Entity.GetBinaryContent()),
ParameterType.RequestBody);
}
diff --git a/main/Smartsheet/Api/Internal/Util/Util.cs b/main/Smartsheet/Api/Internal/Util/Util.cs
index ee97b41..c3d48c6 100644
--- a/main/Smartsheet/Api/Internal/Util/Util.cs
+++ b/main/Smartsheet/Api/Internal/Util/Util.cs
@@ -85,8 +85,11 @@ namespace Smartsheet.Api.Internal.Utility
byte[] buffer = new byte[bufferSize];
int count;
while ((count = reader.Read(buffer, 0, buffer.Length)) != 0)
+ {
ms.Write(buffer, 0, count);
- return ms.ToArray();
+ }
+ ms.Position = 0;
+ return ms.ToArray();
}
}
}
https://stackoverflow.com/questions/23127512
复制