private static string CRLF = "\r\n";
        private static string boundary = "----" + DateTime.Now.Ticks.ToString("x") + "----";
        private static Stream DataStream = new MemoryStream();
        private static byte[] formData;

        public static void APIExamCafePostMultipart(string tockenkey, string title, string memo, string imageurl)
        {
            DataStream = new MemoryStream();
            string token = tockenkey; // 네이버 로그인 접근 토큰
            string header = "Bearer " + token; // Bearer 다음에 공백 추가
            string clubid = cafeid; // 카페의 고유 ID값
            string menuid = menuID; // 메뉴ID값
            string headid = headID; // 말머리

            string url = "https://openapi.naver.com/v1/cafe/" + clubid + "/menu/" + menuid + "/articles";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "multipart/form-data; boundary=" + boundary;
            request.Headers.Add("X-Naver-Client-Id", clientID); // 등록한 어플리케이션 Client ID
            request.Headers.Add("X-Naver-Client-Secret", ScreatID); // 등록한 어플리케이션 Client Secret
            request.Headers.Add("Authorization", header);
            buildParam("subject", title); // 제목
            buildParam("content", memo); // 본문
            buildParam("headid", headid); // 말머리
            buildFileParam("image[0]", imageurl); // 파일 [0]
            //buildFileParam("image[1]", "C:\\test2.jpg"); // 파일 [1]
            buildByteParam(); // Byte Array 생성
            Stream stream = request.GetRequestStream();
            stream.Write(formData, 0, formData.Length); // request 전송
            stream.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string text = reader.ReadToEnd();
            stream.Close();
            response.Close();
            reader.Close();
            Console.WriteLine(text);
        }

 

+ Recent posts

티스토리 툴바