본문 바로가기

자료

[C#/vb.net] WinHttp를 이용한 네이버 SmartEditor 2.0 이미지 업로드

728x90

 

블로그 인기글

● 메이플스토리 신규 룬패치/자동해제 프로그램 [링크 이동]

● 메이플스토리 거짓말탐지기 알림 프로그램 [링크 이동]

C#에서 TensorFlow 사용하는법 [링크 이동]


 Private Function upphoto(ByVal FilePath As String, ByVal href As String) As String
        Dim sqeid As String = "QE_" & UNIX_TIME() & "60998"
        Dim PostData As String
        Dim ImageBinary() As Byte
        Dim Header1 As String = "", Header2 As String = ""
        Dim BHeader1() As Byte, BHeader2() As Byte, Buffer() As Byte
        'Dim Size1 As Long, Size2 As Long, Size3 As Long
        'Dim FN As Integer
        'Dim PostData As String
        Dim boundary As String, Headers As String
        Dim T As String = ""
        Dim URL As String = "https://blog.naver.com/AttachPhotoWebtopForm.nhn?mode=base&blogId=" & _nhnID
        With CreateObject("WinHttp.WinHttpRequest.5.1")
            .Open("GET", URL)
            .SetRequestHeader("Accept", "text/html, application/xhtml+xml, */*")
            .SetRequestHeader("Accept-Language", "ko-KR")
            .SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko")
            .SetRequestHeader("Host", "blog.naver.com")
            .SetRequestHeader("Connection", "Keep-Alive")
            If Len(Cookie) Then .SetRequestHeader("Cookie", Cookie)
            .Send()
            .WaitForResponse() : Application.DoEvents()
            'T = System.Text.Encoding.Default.GetString(.ResponseBody)
            T = .ResponseText
        End With
        'Clipboard.SetText(T)
        Dim sessionKey As String = Split(Split(T, "sessionKey=")(1), "&")(0)
        Dim FileName As String
        FileName = Split(FilePath, "\")(UBound(Split(FilePath, "\")))
        FileName = Split(FileName, ".")(0)
        FileName = textEN(FileName) & "." & Split(FilePath, ".")(UBound(Split(FilePath, ".")))
        FileName = Replace$(FileName, "%", "")
 
        Dim image As System.Drawing.Image = SafeImageFromFile(FilePath)
        Dim FormetType As String = String.Empty
        If image.RawFormat.Guid = System.Drawing.Imaging.ImageFormat.Tiff.Guid Then
            FormetType = "TIFF"
        ElseIf image.RawFormat.Guid = System.Drawing.Imaging.ImageFormat.Gif.Guid Then
            FormetType = "GIF"
        ElseIf image.RawFormat.Guid = System.Drawing.Imaging.ImageFormat.Jpeg.Guid Then
            FormetType = "JPG"
        ElseIf image.RawFormat.Guid = System.Drawing.Imaging.ImageFormat.Bmp.Guid Then
            FormetType = "BMP"
        ElseIf image.RawFormat.Guid = System.Drawing.Imaging.ImageFormat.Png.Guid Then
            FormetType = "PNG"
        ElseIf image.RawFormat.Guid = System.Drawing.Imaging.ImageFormat.Icon.Guid Then
            FormetType = "ICO"
        Else
            Throw New System.ArgumentException("Invalid File Type")
        End If
        image.Dispose()
 
        Header1 &= "-----------------------------66411509834719795442171685220" & vbCrLf
        Header1 &= "Content-Disposition: form-data; name=""image""; filename=""" & FileName & """" & vbCrLf
        Header1 &= "Content-Type: image/" & FormetType.ToLower & vbCrLf
        Header1 &= "" & vbCrLf
        '// 이미지데이터
        Header2 = vbCrLf
        Header2 &= "-----------------------------66411509834719795442171685220" & vbCrLf
        Header2 &= "Content-Disposition: form-data; name=""filename""" & vbCrLf
        Header2 &= "" & vbCrLf
        Header2 &= "_Pngtree_2 5d black monitor illustration_4751253.png" & vbCrLf
        Header2 &= "-----------------------------66411509834719795442171685220" & vbCrLf
        Header2 &= "Content-Disposition: form-data; name=""autorotate""" & vbCrLf
        Header2 &= "" & vbCrLf
        Header2 &= "true" & vbCrLf
        Header2 &= "-----------------------------66411509834719795442171685220" & vbCrLf
        Header2 &= "Content-Disposition: form-data; name=""extractAnimatedCnt""" & vbCrLf
        Header2 &= "" & vbCrLf
        Header2 &= "true" & vbCrLf
        Header2 &= "-----------------------------66411509834719795442171685220" & vbCrLf
        Header2 &= "Content-Disposition: form-data; name=""userId""" & vbCrLf
        Header2 &= "" & vbCrLf
        Header2 &= "" & _nhnID & "" & vbCrLf
        Header2 &= "-----------------------------66411509834719795442171685220--" & vbCrLf
 
 
        '### 이미지 바이너리를 얻는다.
        ImageBinary = IO.File.ReadAllBytes(FilePath)
 
 
        '### 전송할 데이터를 만든다.
        BHeader1 = System.Text.Encoding.UTF8.GetBytes(Header1)
        BHeader2 = System.Text.Encoding.UTF8.GetBytes(Header2)
 
        Dim DS As New MemoryStream
        DS.Write(BHeader1, 0, BHeader1.Length)
        DS.Write(ImageBinary, 0, ImageBinary.Length)
        DS.Write(BHeader2, 0, BHeader2.Length)
 
        DS.Position = 0
        ReDim Buffer(DS.Length)
        DS.Read(Buffer, 0, Buffer.Length) : DS.Close()
 
        URL = "https://blog.upphoto.naver.com/" & sessionKey & "/upload/0"
        With CreateObject("WinHttp.WinHttpRequest.5.1")
            .Open("POST", URL)
            .SetRequestHeader("Host", "blog.upphoto.naver.com")
            .SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0")
            .SetRequestHeader("Accept", "*/*")
            .SetRequestHeader("Accept-Language", "ko-KR,ko;q=0.8,en-US;q=0.5,en;q=0.3")
            .SetRequestHeader("Content-Type", "multipart/form-data; boundary=---------------------------66411509834719795442171685220")
            .SetRequestHeader("Content-Length", Buffer.Length)
            .SetRequestHeader("Origin", "https://blog.upphoto.naver.com")
            .SetRequestHeader("Connection", "keep-alive")
            .SetRequestHeader("Referer", "https://blog.upphoto.naver.com/" & sessionKey & "/startup?mode=base&width=740")
            If Len(Cookie) Then .SetRequestHeader("Cookie", Cookie)
            .Send(Buffer)
            .WaitForResponse() : Application.DoEvents()
            'T = System.Text.Encoding.Default.GetString(.ResponseBody)
            T = .ResponseText
        End With
        '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        '<item>
        '    <index>0</index>
        '    <url>/" & sessionKey & "/0</url>
        '    <path>" & sessionKey & "</path>
        '    <fileName>0</fileName>
        '    <width>500</width>
        '    <height>476</height>    
 
        '    <fileSize>23112</fileSize>
        '    <thumbnail>/" & sessionKey & "/thumbnail/0</thumbnail>
 
 
 
        '    <animatedCnt>1</animatedCnt>
 
        '</item>
 
 
        URL = "https://blog.upphoto.naver.com/" & sessionKey & "/checkHarm/0"
        With CreateObject("WinHttp.WinHttpRequest.5.1")
            .Open("GET", URL)
            .SetRequestHeader("Host", "blog.upphoto.naver.com")
            .SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0")
            .SetRequestHeader("Accept", "application/xml, text/xml, */*; q=0.01")
            .SetRequestHeader("Accept-Language", "ko-KR,ko;q=0.8,en-US;q=0.5,en;q=0.3")
            .SetRequestHeader("Connection", "keep-alive")
            .SetRequestHeader("Referer", "https://blog.upphoto.naver.com/" & sessionKey & "/startup?mode=base&width=740")
            If Len(Cookie) Then .SetRequestHeader("Cookie", Cookie)
            .Send()
            .WaitForResponse() : Application.DoEvents()
            'T = System.Text.Encoding.Default.GetString(.ResponseBody)
            T = .ResponseText
        End With
        '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        '<result>
        '    <isSuccess>true</isSuccess>
        '    <isHarm>false</isHarm>
        '</result>
 
        PostData = "customKeyVersion=0"
        URL = "https://blog.upphoto.naver.com/" & sessionKey & "/save/0"
        With CreateObject("WinHttp.WinHttpRequest.5.1")
            .Open("POST", URL)
            .SetRequestHeader("Host", "blog.upphoto.naver.com")
            .SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0")
            .SetRequestHeader("Accept", "*/*")
            .SetRequestHeader("Accept-Language", "ko-KR,ko;q=0.8,en-US;q=0.5,en;q=0.3")
            .SetRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
            .SetRequestHeader("Content-Length", PostData.Length)
            .SetRequestHeader("Origin", "https://blog.upphoto.naver.com")
            .SetRequestHeader("Connection", "keep-alive")
            .SetRequestHeader("Referer", "https://blog.upphoto.naver.com/" & sessionKey & "/startup?mode=base&width=740")
            If Len(Cookie) Then .SetRequestHeader("Cookie", Cookie)
            .Send(PostData)
            .WaitForResponse() : Application.DoEvents()
            'T = System.Text.Encoding.Default.GetString(.ResponseBody)
            T = .ResponseText
        End With
        '  <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
        '- <item>
        '  <index>0</index> 
        '  <url>/MjAyMDA1MjNfMTk5/MDAxNTkwMjA4MzkxODc5.AiNoZLJeWa2a5EhsbcdtPNyYXNy69o3TlMGERJt1ahog.8CBmBNL5qGsyh6eEZ9lMEDCabXiG7eMfrFs1Hpy75n0g.PNG.program1472/_Pngtree_2_5d_black_monitor_illustration_4751253.png</url> 
        '  <path>/MjAyMDA1MjNfMTk5/MDAxNTkwMjA4MzkxODc5.AiNoZLJeWa2a5EhsbcdtPNyYXNy69o3TlMGERJt1ahog.8CBmBNL5qGsyh6eEZ9lMEDCabXiG7eMfrFs1Hpy75n0g.PNG.program1472</path> 
        '  <fileName>_Pngtree_2_5d_black_monitor_illustration_4751253.png</fileName> 
        '  <width>500</width> 
        '  <height>476</height> 
        '  <fileSize>23112</fileSize> 
        '  <thumbnail>/MjAyMDA1MjNfMTk5/MDAxNTkwMjA4MzkxODc5.AiNoZLJeWa2a5EhsbcdtPNyYXNy69o3TlMGERJt1ahog.8CBmBNL5qGsyh6eEZ9lMEDCabXiG7eMfrFs1Hpy75n0g.PNG.program1472/_Pngtree_2_5d_black_monitor_illustration_4751253.png</thumbnail> 
        '  </item>
        Dim width As Integer = Split(Split(T, "<width>")(1), "</width>")(0)
        Dim height As Integer = Split(Split(T, "<height>")(1), "</height>")(0)
        Dim thumbnail As String = Split(Split(T, "<thumbnail>")(1), "</thumbnail>")(0)
 
        URL = "https://blog.upphoto.naver.com/" & sessionKey & "/finish"
        With CreateObject("WinHttp.WinHttpRequest.5.1")
            .Open("GET", URL)
            .SetRequestHeader("Host", "blog.upphoto.naver.com")
            .SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0")
            .SetRequestHeader("Accept", "*/*")
            .SetRequestHeader("Accept-Language", "ko-KR,ko;q=0.8,en-US;q=0.5,en;q=0.3")
            .SetRequestHeader("Connection", "keep-alive")
            .SetRequestHeader("Referer", "https://blog.upphoto.naver.com/" & sessionKey & "/startup?mode=base&width=740")
            If Len(Cookie) Then .SetRequestHeader("Cookie", Cookie)
            .Send()
            .WaitForResponse() : Application.DoEvents()
            'T = System.Text.Encoding.Default.GetString(.ResponseBody)
            T = .ResponseText
        End With
        '  <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
        '- <result>
        '  <isSuccess>true</isSuccess> 
        '  </result>
 
        Dim HTML As String = ""
        If href.Length > 0 Then HTML &= "<a href=""" & href & """ target=""_self"">"
        HTML &= "<img id=""se_object_" & UNIX_TIME() & "5323"" "
        HTML &= "src=""https://blogfiles.pstatic.net" & thumbnail & """ "
        HTML &= "class=""__se_object"" "
        HTML &= "s_type=""attachment"" "
        HTML &= "s_subtype=""photo"" "
        HTML &= "rwidth=""" & width & """ "
        HTML &= "rheight=""" & height & """ "
        HTML &= "imgqe=""true"" "
        HTML &= "title=""" & thumbnail.Split("/"c).Last & """ "
        HTML &= "jsonvalue=""%7B%7D"" "
        '// 이미지 가운데 정렬을 이해 "display: block; margin-left: auto; margin-right: auto; "가 들어감
        HTML &= "style=""display: block; margin-left: auto; margin-right: auto; width: " & width & "px; height: " & height & "px; border-color: rgb(0, 0, 0);"" "
        HTML &= "sqeid=""" & sqeid & """ "
        HTML &= "width=""" & width & """ "
        HTML &= "height=""" & height & """ "
        HTML &= "align="""">"
        If href.Length > 0 Then HTML &= "</a>"
        Return HTML
    End Function
728x90