2017-06-08 37 views
1

我想HttpClient的PostAsync()請求發送給公司內部的SharePoint網站,但與禁止錯誤的返回響應。我有加載網站的所有必要訪問權限,並且還將所需的標頭傳遞給HttpClient對象。與HttpClient的PostAsync調用的SharePoint()結果爲禁止響應

這裏是代碼片段。

HttpClient client = new System.Net.Http.HttpClient (new HttpClientHandler { UseDefaultCredentials = true }); 

client.BaseAddress = new Uri (string.Format (API_URL, p_siteNumber)); 
client.DefaultRequestHeaders.Accept.Add (new MediaTypeWithQualityHeaderValue (@"application/atom+xml")); 
client.DefaultRequestHeaders.TryAddWithoutValidation ("Accept-Encoding", "gzip, deflate"); 
client.DefaultRequestHeaders.TryAddWithoutValidation ("Accept-Language", "en-US, en;q=0.8, hi;q=0.6"); 
client.DefaultRequestHeaders.TryAddWithoutValidation ("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0"); 
client.DefaultRequestHeaders.TryAddWithoutValidation ("Accept-Charset", "ISO-8859-1"); 

HttpResponseMessage httpResponse = await client.PostAsync (urlHttpPost, new StringContent (string.Empty)); 

string response = await httpResponse.Content.ReadAsStringAsync(); 

任何人都可以幫助我嗎? 在此先感謝。

+0

我們不能真正幫助多少根據你給我們什麼小。要麼你錯過了一個標題或者某種類型的服務器期望的cookie或post值。我建議使用的軟件,如提琴手嗅出了有效的請求,並應用到你的代碼。 – ThePerplexedOne

+0

如果你從https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en用相同的標題同樣的要求和內容,你會得到相同的結果呢? – mjwills

+1

你使用了什麼網址?你想做什麼?如果您的帳戶沒有使用權限,則無法訪問圖書館或列表。您是否嘗試過使用[客戶端對象模型(https://dev.office.com/sharepoint/docs/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code)?開始使用它來消除請求的問題,如無效的網址或壞頭 –

回答

1

我遇到了同樣的問題,我想發送文件和一些字符串的內容。

所以下面的代碼幫助了我!

using (var client = new HttpClient()) 
     { 
      //client.DefaultRequestHeaders.Add("User-Agent", "CBS Brightcove API Service"); 
      string authorization = GenerateBase64(); 
      client.DefaultRequestHeaders.Add("Authorization", authorization); 


      using (var content = new MultipartFormDataContent()) 
      { 

       string fileName = Path.GetFileName(textBox1.Text); 

       //Content-Disposition: form-data; name="json" 
       var stringContent = new StringContent(InstancePropertyObject); 
       stringContent.Headers.Remove("Content-Type"); 
       stringContent.Headers.Add("Content-Type", "application/json"); 
       stringContent.Headers.Add("Content-Disposition", "form-data; name=\"instance\""); 
       content.Add(stringContent, "instance"); 

       var fileContent = new ByteArrayContent(filecontent); 
       fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") 
       { 
        FileName = fileName 
       }; 
       content.Add(fileContent); 

       var result = client.PostAsync(targetURL, content).Result; 
      } 
     } 
相關問題