I'm not a programmer. So be gentle... I'm trying to write a bitly shortner in vb.net
What i did:
-created the account in bitly
-created the token
I searched for code and want to use those snippets:
'String for token
Dim tokenString As String = "xxxxxxxxxmytokenxxxxxxxx"
'Stream for the responce
Dim responseStream As System.IO.Stream
'Stream reader to read the stream to a string
Dim stringStreamReader As System.IO.StreamReader
'String to be read to
Dim responseString As String
'The webrequest that is querying
Dim webRequest As WebRequest = WebRequest.Create("https://api-ssl.bitly.com/v4/shorten")
'The collection of headers
Dim webHeaderCollection As WebHeaderCollection = webRequest.Headers
'Adding a header
webHeaderCollection.Add("xxxMyUsernamexxx:Bearer " tokenString)
'The web responce
Dim webResponce As HttpWebResponse = CType(webRequest.GetResponse(), HttpWebResponse)
'Reading the web responce to a stream
responseStream = webResponce.GetResponseStream()
'Initializing the stream reader with our stream
stringStreamReader = New StreamReader(responseStream)
'Reading the stream to our string
responseString = stringStreamReader.ReadToEnd.ToString
'Ending the web responce
webResponce.Close()
But i get this error:
Has someone a code snippet for me which works already?
Thank you for any advise :-)
CodePudding user response:
A 405 HTTP Response is also referred to as Method Not Allowed. Generally this happens when you send the incorrect method type. So for example, the endpoint is setup to accept GET requests but you've sent a POST request.
By default, the WebRequest will submit a GET but it looks like the API is expecting a POST. You can specify the method manually by using:
webRequest.request.Method = "POST"
CodePudding user response:
This is one those answers where I entered a lot of code directly into the reply window, so while the general theme here is correct there WILL still be some bugs you need to work through:
Const tokenString As String = "xxxxxxxxxmytokenxxxxxxxx"
Const myUserName As String = "xxxMyUsernamexxx"
Const myGroupID As String = "xxxxxxxxxxx"
Public Shared Function ShortenUrl(urlToShorten As String) As String
Using rq As WebRequest = WebRequest.Create("https://api-ssl.bitly.com/v4/shorten")
Dim data As String = $"{{""group_guid"": ""{myGroupID}"", ""long_url"": ""{urlToShorten}""}}"
rq.Headers.Add($"{myUserName}:Bearer {tokenString}")
rq.Method = "POST"
rq.ContentType = "application/json"
rq.ContentLength = data.Length
Using requestStream As Stream = rq.GetRequestStream()
Dim postBuffer = Encoding.ASCII.GetBytes(post);
rq.Write(postBuffer, 0, postBuffer.Length);
End Using
Using rsp As HttpWebResponse = DirectCast(rq.GetResponse(), HttpWebResponse), _
str As Stream = rsp.GetResponseStream()), _
rdr As New StreamReader(str)
Dim json As String = rdr.ReadToEnd()
Return Regex.Match(json, """link"": ?""(?<link>[^,;] )""").Groups("link").Value
End Using
End Using
End Function
CodePudding user response:
Thank you I will give my best to get the code run. I let you know my progress.
Thank you very much :-)