WinHttp [vb.net] WinHttp를 이용한 네이버 블로그 이미지 업로드(Uploading Naver Blog Image Using…
페이지 정보
본문
Private _Socat As Object
Public Function Socat() As Object
If _Socat Is Nothing Then _Socat = CreateObject("WinHttp.WinHttpRequest.5.1")
Return _Socat
End Function
Private Function upphoto(ByVal appCode As String, ByVal token As String, ByVal userId As String, ByVal FilePath As String) As String
Dim ImageBinary() As Byte
Dim Header1 As String = "", Header2 As String = ""
Dim BHeader1() As Byte, BHeader2() As Byte, Buffer() As Byte
Dim URL As String, T As String, O As Object
Const boundary As String = "WebKitFormBoundaryHyQAyLP7sAQa62le"
If Cookie Is Nothing Then Cookie = ""
'// 이미지 첨부
With Socat()
.Open("GET", URL)
.SetRequestHeader("Host", "platform.editor.naver.com")
.SetRequestHeader("Connection", "keep-alive")
.SetRequestHeader("sec-ch-ua", """Chromium"";v=""106"", ""Google Chrome"";v=""106"", ""Not;A=Brand"";v=""99""")
.SetRequestHeader("Pragma", "no-cache")
.SetRequestHeader("SE-Authorization", token)
'.SetRequestHeader("SE-App-Id", "SE-7abd6f55-6b28-41a0-b08e-a6d69e63e5ea")
.SetRequestHeader("sec-ch-ua-mobile", "?0")
.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36")
.SetRequestHeader("Accept", "application/json")
.SetRequestHeader("sec-ch-ua-platform", """Windows""")
.SetRequestHeader("Origin", "https://blog.naver.com")
.SetRequestHeader("Sec-Fetch-Site", "same-site")
.SetRequestHeader("Sec-Fetch-Mode", "cors")
.SetRequestHeader("Sec-Fetch-Dest", "empty")
.SetRequestHeader("Referer", "https://blog.naver.com/PostWriteForm.naver?blogId=" & userId & "&Redirect=Write&categoryNo=0&redirect=Write&widgetTypeCall=true&directAccess=false")
.SetRequestHeader("Accept-Language", "ko")
'If Len(Cookie) Then .SetRequestHeader("Cookie", Cookie)
.Send()
.WaitForResponse() : Application.DoEvents()
'Cookie = SetCookie(Cookie, .GetAllResponseHeaders)
'T = System.Text.Encoding.Default.GetString(.ResponseBody)
T = .ResponseText
End With
O = JsonConvert.DeserializeObject(T) ': Clipboard.SetText(O.ToString) : Stop
'{"sessionKey":"MjAyMjEwMTIxNDA0NjQHMTY2NTU1MTc1ODIwOAdibG9nX2VkaXRvcjIHcHJvZ3JhbTE0NzIHMAcyBzdmOTFjY2M4YTFjOGM5ZmYyODVmMTkzMjNiYzNjZjM4","isSuccess":true}
Dim sessionKey As String = O("sessionKey")
URL = "https://blog.upphoto.naver.com/" & sessionKey & "/simpleUpload/0?userId=" & userId & "&extractExif=true&extractAnimatedCnt=true&autorotate=true&extractDominantColor=false&type=&customQuery=&denyAnimatedImage=false&skipXcamFiltering=false"
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 = "------" & boundary & vbCrLf
Header1 &= "Content-Disposition: form-data; name=""image""; filename=""" & FileName & """" & vbCrLf
Header1 &= "Content-Type: image/" & FormetType.ToLower & vbCrLf
Header1 &= "" & vbCrLf
Header2 = vbCrLf
Header2 &= "------" & boundary & "--" & 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()
With Socat()
.Open("POST", URL)
.SetRequestHeader("Host", "blog.upphoto.naver.com")
.SetRequestHeader("Connection", "keep-alive")
.SetRequestHeader("sec-ch-ua", """Chromium"";v=""106"", ""Google Chrome"";v=""106"", ""Not;A=Brand"";v=""99""")
.SetRequestHeader("sec-ch-ua-platform", """Windows""")
.SetRequestHeader("sec-ch-ua-mobile", "?0")
.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36")
.SetRequestHeader("Content-Type", "multipart/form-data; boundary=----" & boundary)
.SetRequestHeader("Accept", "*/*")
.SetRequestHeader("Origin", "https://blog.naver.com")
.SetRequestHeader("Sec-Fetch-Site", "same-site")
.SetRequestHeader("Sec-Fetch-Mode", "cors")
.SetRequestHeader("Sec-Fetch-Dest", "empty")
'.SetRequestHeader("Referer", "https://blog.naver.com/PostWriteForm.naver?blogId=" & userId & "&Redirect=Write&categoryNo=0&redirect=Write&widgetTypeCall=true&directAccess=false")
.SetRequestHeader("Accept-Language", "ko")
.SetRequestHeader("Content-Length", Buffer.Length)
.Send(Buffer)
.WaitForResponse() : Application.DoEvents()
'Cookie = SetCookie(Cookie, .GetAllResponseHeaders)
'T = System.Text.Encoding.Default.GetString(.ResponseBody)
T = .ResponseText ': Clipboard.SetText(T) : Stop
End With
T = T.Replace(""", """")
T = T.Replace("<exif><![CDATA[", "<exif>")
T = T.Replace("]]></exif>", "</exif>")
T = XmlToJSON(T) ': Clipboard.SetText(T) : Stop
O = JsonConvert.DeserializeObject(T)
O("item")("exif") = JsonConvert.DeserializeObject(O("item")("exif"))
'Clipboard.SetText(O.ToString) : Stop
'Clipboard.SetText(String.Concat(O("item")("baseUrl"), O("item")("url"))) : Stop
Return JsonConvert.SerializeObject(O("item"))
End Function
첨부파일
-
JSON.vb (4.4K)
4회 다운로드 | DATE : 2022-10-12 15:47:42
- 다음글[WinHttp] WinHttpRequest 22.08.19
댓글목록
등록된 댓글이 없습니다.