본문 바로가기
VB.net

네이버 OpenAPI 이용한 Shorten URL

by 호야호잇 2020. 5. 29.

Private Sub RipsWareImageButtonBase2_Click(sender As Object, e As EventArgs) Handles RipsWareImageButtonBase2.Click

'URL 복사

Dim NaverAPIURL As String = "https://openapi.naver.com/v1/util/shorturl"

Dim LongURL As String = "url=" & Uri.EscapeDataString(TextBox1.Text)

Dim ChangeURL As String = RequestApi_PostUtf8(NaverAPIURL, LongURL)

Dim ShortURL As String = Split(Split(Replace(ChangeURL, """", ""), "url:")(1), ",")(0)

 

'Debug.Print(ShortURL)

Dim smsURL As String = "" smsURL += mms_msg & vbCrLf

smsURL += ShortURL Clipboard.SetText(smsURL)

 

Dim msg As String = mms_msg & vbCrLf msg += "URL 복사완료"

MsgBox("URL 복사 완료", MsgBoxStyle.Information, "보이는 기변 플래너")

 

End Sub

 

 

 

Public Function RequestApi_PostUtf8(ByVal url As String, ByVal Body As String) As String

 

Dim UserAgent As String

' Dim UrlDataRequest As String = "http://wwwws2=&t=VOD&start=1rows=1&biz_type=P" UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36"

Dim request As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)

request.Method = "POST"

'Dim byteDataParams As Byte() = Encoding.UTF8.GetBytes(Body)

Dim byteDataParams As Byte() = Encoding.UTF8.GetBytes(Body)

request.ContentType = "application/x-www-form-urlencoded"

request.ContentLength = byteDataParams.Length

' request.Headers.Add("Accept-Encoding", "gzip,deflate")

request.Headers.Add("X-Naver-Client-Id", "TcEU8R0wlpwjjXPWGSYc") '개발자센터에서 발급받은 Client ID request.Headers.Add("X-Naver-Client-Secret", "TokzsGOsLf") ' 개발자센터에서 발급받은 Client

request.UserAgent = UserAgent

Dim st As Stream = request.GetRequestStream()

st.Write(byteDataParams, 0, byteDataParams.Length) st.Close()

Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)

Dim stream As Stream = response.GetResponseStream()

Dim reader As StreamReader = New StreamReader(stream, Encoding.UTF8)

RequestApi_PostUtf8 = reader.ReadToEnd() s

tream.Close()

response.Close()

reader.Close()

 

End Function

'VB.net' 카테고리의 다른 글

전역 오류 확인 및 전달 로직  (0) 2020.09.24
오류 확인 로직_테스트중  (0) 2020.09.23
프로세스 확인  (0) 2020.05.08
중복값 제거 방법 (List 이용)  (0) 2020.04.07
파일 속성 가져오기  (0) 2020.04.01