2012-03-20 3 views
5

ics 파일을 REST API에 업로드해야합니다. 주어진 유일한 예는 컬 명령입니다.REST API에 파일을 업로드 할 때 .NET에 해당하는 컬?

컬을 사용하여 파일을 업로드하는 데 사용되는 명령은 다음과 같습니다

curl --user {username}:{password} --upload-file /tmp/myappointments.ics http://localhost:7070/home/john.doe/calendar?fmt=ics 

나는 C#에서 HttpWebRequest를 사용하여이 작업을 수행 할 수 있습니까?

또한 실제 파일이 아닌 문자열로만 사용할 수 있습니다.

+0

http://stackoverflow.com/questions/2360832/using-net-to-post-a-file-to-server-httpwebrequest-or-webclient 외모 : 저는 여기에 사용되는 코드의 예입니다 비슷한 일을하는 것 – dash

답변

5

나는 해결책을 얻을 수 있었다. 특이한 점은 요청 대신 POST 대신 PUT으로 메소드를 설정하는 것이 었습니다.

var strICS = "text file content"; 

byte[] data = Encoding.UTF8.GetBytes (strICS); 

HttpWebRequest request = (HttpWebRequest)WebRequest.Create ("http://someurl.com"); 
request.PreAuthenticate = true; 
request.Credentials = new NetworkCredential ("username", "password");; 
request.Method = "PUT"; 
request.ContentType = "text/calendar"; 
request.ContentLength = data.Length; 

using (Stream stream = request.GetRequestStream()) { 
    stream.Write (data, 0, data.Length); 
} 

var response = (HttpWebResponse)request.GetResponse(); 
response.Close(); 
관련 문제