这是Stackoverflow中my earlier question之一的后续问题。使用C#Sharepoint 2013 REST API调用
我试图连接到SharePoint Online的前提利用HttpClient的和SharePoint 2013 REST API来获取“文档库”中的文件。
所有我做这里是使用HttpClient的一个简单的匿名HTTP GET调用。
的代码如下:
System.Net.Http.HttpClient _Client = new System.Net.Http.HttpClient();
_Client.BaseAddress = new Uri("https://test.sharepoint.com/_vti_bin/ListData.svc");
_Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/atom+xml"));
_Client.DefaultRequestHeaders.Add("auth_user", "[email protected]");
_Client.DefaultRequestHeaders.Add("auth_pass", "test");
_Client.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
HttpResponseMessage resp = _Client.GetAsync(new Uri("https://test.sharepoint.com/_vti_bin/ListData.svc/Documents()")).Result;
string respString = resp.Content.ReadAsStringAsync().Result;
,我收到以下错误:
无法读取从传输连接数据:连接被关闭。
堆栈跟踪如下:
[IOException: Unable to read data from the transport connection: The connection was closed.]
System.Net.ConnectStream.EndRead(IAsyncResult asyncResult) +6501654
System.Net.Http.WebExceptionWrapperStream.EndRead(IAsyncResult asyncResult) +30
System.Net.Http.StreamToStreamCopy.BufferReadCallback(IAsyncResult ar) +54
[HttpRequestException: Error while copying content to a stream.]
[AggregateException: One or more errors occurred.]
System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) +3569193
System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) +73
System.Threading.Tasks.Task`1.get_Result() +10522673
WebApp.Test.Page_Load(Object sender, EventArgs e) in @Location
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772
我已经通过各种blogs/articles去得到一些想法来解决issue.But我cudn't找到任何解决办法呢。
对此有何看法?
您是否正在从ASP.NET网站执行此操作?我怀疑你的.Result电话有些奇怪。你有没有尝试过使用异步/等待? –
是的,我正在从ASP.NET网站执行此操作。 – Bisileesh