简体   繁体   English

使用HttpClient PostAsync()调用SharePoint导致禁止响应

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

I am trying to send HttpClient PostAsync() request to company's internal sharepoint site but its returning response with forbidden error. 我正在尝试将HttpClient PostAsync()请求发送到公司的内部共享点站点,但是其返回的响应带有被禁止的错误。 I have all necessary access permission for site to load and have also passed required headers to the HttpClient object. 我具有网站加载所需的所有必要访问权限,并且还已将必需的标头传递给HttpClient对象。

Here is code snippet. 这是代码片段。

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

Can anyone help me with this? 谁能帮我这个? Thanks in advance. 提前致谢。

I ran into the same problem I wanted to send the file and some string contents with it. 我遇到了与要发送文件和一些字符串内容相同的问题。

so below code helped me!! 所以下面的代码对我有帮助!

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