2017-02-28 4 views
1

WebClient를 사용하여 webapi로 데이터를 보내려고합니다. 코드를 실행하면 제목에 예외가 표시됩니다. 누군가 나를 도울 수 있습니까? 당신은 HttpWebRequest 사용할 수있는 헤더 정의'Content-Length'헤더는 해당 속성이나 메서드를 사용하여 수정해야합니다. 매개 변수 이름 : 이름

using (WebClient wc = new WebClient()) 
      { 
       wc.Headers[HttpRequestHeader.ContentType] = "application/json"; 
       wc.Headers[HttpRequestHeader.Authorization] = headerString; 

       data = Encoding.UTF8.GetBytes(jsonData); 
       string contentLength = data.Length.ToString(); 

       wc.Headers[HttpRequestHeader.ContentLength] = contentLength; 
       wc.Headers[HttpRequestHeader.Accept] = "application/json"; 

       wrCache = new CredentialCache(); 
       wrCache.Add(new Uri(URI), "Basic", new NetworkCredential("user1", "[email protected]")); 

       wc.Credentials = wrCache; 


       byte[] htmlResult = wc.UploadData(URI, "POST", data); 

답변

0

내 코드의

여기에 일부입니다. 예 :

private static HttpStatusCode UploadSharepointFile(string fileName) 
{ 
     // Read file from path 
     byte[] buffer = File.ReadAllBytes(fileName); 

     HttpWebRequest http = 
      WebRequest.CreateHttp($"https://google.com"); 

     http.Method = "POST"; 

     http.Credentials = (ICredentials) Program.cred; 

     http.Headers.Add("Accept", "application/json"); 
     http.ContentLength = buffer.Length; 
     http.GetRequestStream().Write(buffer, 0, buffer.Length); 

     HttpWebResponse response = (HttpWebResponse) http.GetResponse(); 
     return response.StatusCode; 
} 
관련 문제