C# 네이버 메일 이미지 업로드 코드
예외처리는 하나도 하지 않았습니다. 손봐야 할부분이 몇군데 있는것 같지만 그냥 올립니다.
public static string NaverImageUpload(string path, CookieContainer cookie) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://mail.naver.com/write/image/upload"); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); byte[] data = new byte[fs.Length]; fs.Read(data, 0, data.Length); fs.Close(); Stream DataStream = new MemoryStream(); string boundary = "-----------------------------36932931913641"; string postData = boundary + "\r\nContent-Disposition: form-data; name=\"attachID\"\r\n\r\ncmbZKAKwKAUTFob9BqvmbXErW4kd7qM9FoK-FxgdFqtZKh.."; postData += "\r\n" + boundary + "\r\nContent-Disposition: form-data; name=\"file\"; filename=\"" + Path.GetFileName(path) + "\"\r\nContent-Type: image/png\r\n\r\n"; string footer = "\r\n-----------------------------36932931913641--\r\n"; DataStream.Write(Encoding.UTF8.GetBytes(postData), 0, Encoding.UTF8.GetByteCount(postData)); DataStream.Write(data, 0, data.Length); DataStream.Write(Encoding.UTF8.GetBytes("\r\n"), 0, 2); DataStream.Write(Encoding.UTF8.GetBytes(footer), 0, Encoding.UTF8.GetByteCount(footer)); DataStream.Position = 0; byte[] formData = new byte[DataStream.Length]; DataStream.Read(formData, 0, formData.Length); DataStream.Close(); request.Method = "POST"; request.ContentType = "multipart/form-data; boundary=---------------------------36932931913641"; request.Referer = "http://mail.naver.com/iframe/upload/html/attachPhoto.html"; request.ContentLength = formData.Length; request.CookieContainer = cookie; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(formData, 0, formData.Length); requestStream.Close(); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream resStream = response.GetResponseStream(); StreamReader readStream = new StreamReader(resStream, Encoding.Default); string result = readStream.ReadToEnd(); readStream.Close(); resStream.Close(); return "http://mail.naver.com/write/image/path/?attachID=cmbZKAKwKAUTFob9BqvmbXErW4kd7qM9FoK-FxgdFqtZKh..&cid=" + Regex.Split(result, "cid : \"(.+?)\"")[1]; }
'소스 관련' 카테고리의 다른 글
2013-02-20 FTP 파일 업로더 (0) | 2013.02.20 |
---|---|
비주얼 베이직 6.0 멀티파트 관련 클래스 모듈 (0) | 2013.02.14 |
Visual Basic 6으로 만들어진 추첨기 코드 (0) | 2013.02.05 |
웹 관련 함수 생성기 (1) | 2013.01.20 |
정규 표현식 테스터(Regular Expression Tester) (3) | 2013.01.03 |