簡體   English   中英

使用HttpClient PostAsync()調用SharePoint導致禁止響應

[英]Calling SharePoint with HttpClient PostAsync() results into forbidden response

我正在嘗試將HttpClient PostAsync()請求發送到公司的內部共享點站點,但是其返回的響應帶有被禁止的錯誤。 我具有網站加載所需的所有必要訪問權限,並且還已將必需的標頭傳遞給HttpClient對象。

這是代碼片段。

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 ();

誰能幫我這個? 提前致謝。

我遇到了與要發送文件和一些字符串內容相同的問題。

所以下面的代碼對我有幫助!

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; 
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM