PUT API call is deleting record instead of updating - vba

I am trying to update data in API using PUT request, but instead its deleting that particular row.
I see some forum to include saving option but where am i supposed to do it. if i do requesturl/save then i get error of "method not value"
my code
Try
Dim myRequest As HttpWebRequest = DirectCast(WebRequest.Create(requestURL), HttpWebRequest)
With myRequest
.Method = "PUT"
.ContentType = "application/json"
.Headers.Add("Authorization", "Bearer " & authHeaderString)
End With
Using streamWriter = New StreamWriter(myRequest.GetRequestStream())
streamWriter.Write(jsonobj)
' streamWriter.Close()
End Using
Return myRequest
Catch ex As Exception
sMessage = ex.Message
Return Nothing
End Try

Related

Connect to WebRequest Server

I'm trying to connect to TNT's server for making XML request.
First I'm trying to just make a successful connection (before trying to send anything).
According to TNT everything is set up ok. So I'm thinking it's on my end.
I'm using the code below. But it just comes back with, that it's unauthorized.
Dim request As WebRequest = WebRequest.Create("https://express.tnt.com/expressconnect/track.do?version=3")
Dim authInfo As String = ("username" + (":" + "password"))
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo))
request.Headers("Authorization") = ("Basic " + authInfo)
request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested
request.ContentType = "application/x-www-form-urlencoded"
request.Method = "POST"
Try
Dim response As WebResponse = request.GetResponse
Console.Write(response)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.ReadLine()
Can anyone help or shed any light on what i am doing wrong?

Betfair NG API Navigation Application Menu error

I am trying to create a Betfair app written in VB.NET using Betfair NG API.
I am trying to request the navigation menu in VB.NET utilizing the following instructions, here, but I get error 400 Bad request. Could someone send me a code for doing so?
Sub GetNavigatorManu(ByVal SessToken As String, ByVal AppKey As String)
Dim request As HttpWebRequest = Nothing
Dim response As HttpWebResponse = Nothing
Dim strResponseStatus As String = ""
Try
request = WebRequest.Create(New Uri("https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json"))
request.Accept = "application/json"
request.Method = "GET"
request.KeepAlive = True
request.ContentType = "application/json"
request.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
request.Headers.Add("Accept-Encoding", "gzip,deflate")
request.Headers.Add("X-Authentication", SessToken)
request.Headers.Add("X-Application", AppKey)
response = request.GetResponse()
Display the status below
strResponseStatus = "Status is " & CType(response, HttpWebResponse).StatusCode
Catch ex As Exception
MsgBox("CreateRequest Error" & vbCrLf & ex.Message)
End Try
MsgBox(strResponseStatus)
End Sub

Error Handling with HTTPWebRequests, VB.NET

I've looked around and couldn't find an answer so I thought i'd ask it here.
I have a program the calls an API with login info passed in the header and the server returns some info upon authentication. The problem is if the user creds are incorrect, the program crashes and throws an exception. The error is 401. How do it catch this, and tell the user?
My code is:
Dim request = TryCast(System.Net.WebRequest.Create("https://myurl.com"), System.Net.HttpWebRequest)
request.Method = "GET"
request.Headers.Add("authorization", "Bearer " + AccessToken)
request.ContentLength = 0
Dim responseContent As String
Using response = TryCast(request.GetResponse(), System.Net.HttpWebResponse)
Using reader = New System.IO.StreamReader(response.GetResponseStream())
responseContent = reader.ReadToEnd()
MessageBox.Show(responseContent)
End Using
End Using
Thanks.
Use a try catch statement
try
'My error prone code
catch ex as Exception
'Handle my error
end try
Try
Dim request = TryCast(System.Net.WebRequest.Create("https://myurl.com"), System.Net.HttpWebRequest)
request.Method = "GET"
request.Headers.Add("authorization", "Bearer " + AccessToken)
request.ContentLength = 0
Dim responseContent As String
Using response = TryCast(request.GetResponse(), System.Net.HttpWebResponse)
Using reader = New System.IO.StreamReader(response.GetResponseStream())
responseContent = reader.ReadToEnd()
MessageBox.Show(responseContent)
End Using
End Using
Catch ex As Exception
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

VB.NET - Checking FTP directory exists always returns true

Can anybody tell me why the function below always returns true, even if the FTP directory in question does not exist?
The value of directoryURL which I pass in is of the form:
ftp://ip_address/directory/subdirectory/
and has a trailing forward slash.
Public Function DoesDirectoryExist(directoryUrl As String) As Boolean
' Check that the target URL is properly formatted
If Not directoryUrl.StartsWith("ftp://") Then directoryUrl = "ftp://" & directoryUrl
' Create a web request
Dim request As FtpWebRequest = DirectCast(WebRequest.Create(directoryUrl), FtpWebRequest)
request.Credentials = New NetworkCredential(_userName, _password)
request.Method = WebRequestMethods.Ftp.ListDirectory
' Try and list the contents of the directory
Try
Using response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
' We have been succesful so the directory exists
Return True
End Using
Catch ex As WebException
Dim response As FtpWebResponse = DirectCast(ex.Response, FtpWebResponse)
If response.StatusCode = FtpStatusCode.ActionNotTakenFileUnavailable Then
Return False
Else
Throw New ApplicationException("Unable to determine if FTP directory exists.")
End If
End Try
End Function
Weird. This works for me (I don't cast the request but I guess that shouldn't matter). This is the code I usually rely on:
Dim response As FtpWebResponse = request.GetResponse()
Using (response)
found = True
End Using
The alternative you have is reading the list of directories:
Using sr As New System.IO.StreamReader(response.GetResponseStream())
Using sw As New System.IO.StreamWriter("tempfile", False)
sw.Write(sr.ReadToEnd())
End Using
End Using
In the worst scenario, it should help you to tackle the problem (e.g., it always founds a directory called "ghost", which you might use to trigger the not-found).
Method 1
Public Function DirectoryExists(directory As String) As Boolean
Dim directoryExists__1 As Boolean
Dim request = DirectCast(WebRequest.Create(directory), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.ListDirectory
request.Credentials = New NetworkCredential("user", "pass")
Try
Using request.GetResponse()
directoryExists__1 = True
End Using
Catch generatedExceptionName As WebException
directoryExists__1 = False
End Try
Return directoryExists__1
End Function
Method 2
If Not DirectoryExists("ftp://" + FTPSettings.IP + "/" + lo_ScreenShotPath) Then
reqFTP = DirectCast(FtpWebRequest.Create(New Uri("ftp://" + FTPSettings.IP + "/" + lo_ScreenShotPath)), FtpWebRequest)
End If
i hope may i help this...

How can I read the response from a web request when the Status is not 200?

I am having difficulty getting the response text from a HTTP web request in vb.net when I get a web exception.
This is the code I am doing it with.
Try
myWebResponse = CType(request.GetResponse(), HttpWebResponse)
myStreamReader = New StreamReader(myWebResponse.GetResponseStream())
ResponseText = myStreamReader.ReadToEnd
If myWebResponse.StatusCode = HttpStatusCode.Accepted Or myWebResponse.StatusCode = 200 Then
SendResult = True 'Sent
SendStatus = 1 'message sent successfully
Try
Integer.TryParse(myWebResponse.Headers("Number-Of-MT-PDU"), num_MT_PDU)
Catch ex As Exception
End Try
Else
SendStatus = 2 'message processed but not sent successfully
End If
Catch e As WebException
If (e.Status = WebExceptionStatus.ProtocolError) Then
Dim response As WebResponse = e.Response
Using (response)
Dim httpResponse As HttpWebResponse = CType(response, HttpWebResponse)
statusCode = httpResponse.StatusCode
Try
myStreamReader = New StreamReader(response.GetResponseStream())
Using (myStreamReader)
ResponseText = myStreamReader.ReadToEnd & "Status Description = " & HttpWebResponse.StatusDescription
End Using
Catch ex As Exception
Logger.LogError(Me, ex)
End Try
End Using
Annoyingly, the API I am contacting uses a 404 as a valid response. If I put the request in a browser some message text will be displayed. I want to be able to use that text in my program. I can not simply use the error code to determine actions as I don't think I can differentiate between a valid 404 response and an actual error.
In the code this line
myWebResponse = CType(request.GetResponse(), HttpWebResponse)
throws an exception.
In the exception I can get the 404 code and the description but not the response stream. It is always null.
If I get a 200 response I get the text in the Response stream no problem.
In the web exception response object (in Visual Studios debugger) I have checked the headers and the object values and can't find the response text anywhere. If I stick the request URL in a browser I get response text back even though it is a 404.
The raw response in fiddler:
HTTP/1.1 404 Not Found Connection: close Content-Type: text/plain; charset=UTF-8 Content-Length: 35 "The response Message"
Any ideas on how I can get "The response Message" in my program? I have to use .Net on the server.
Thanks for any help anybody can give.
This LINQPad query works fine, dumping the HTML provided by my web server's "Not Found" error web page:
Dim rq = System.Net.WebRequest.Create(New Uri("http://localhost/test"))
Try
Dim rs = rq.GetResponse
rs.Dump
Catch Ex As System.Net.WebException
Dim rs = Ex.Response
Call (New StreamReader(rs.GetResponseStream)).ReadToEnd.Dump
End Try
FYI Your code works for me, except the presumed typo re HttpWebResponse.StatusDescription (and commenting out "unrelated stuff"), again as a LINQPad query (in .NET 4.0):
Dim request = WebRequest.Create("http://localhost/test")
Dim myStreamReader As StreamReader
Dim SendStatus As Integer = -1
Dim statusCode As HttpStatusCode
Dim ResponseText As String
Try
Dim myWebResponse = CType(request.GetResponse(), HttpWebResponse)
myStreamReader = New StreamReader(myWebResponse.GetResponseStream())
ResponseText = myStreamReader.ReadToEnd
If myWebResponse.StatusCode = HttpStatusCode.Accepted Or myWebResponse.StatusCode = 200 Then
'SendResult = True 'Sent
SendStatus = 1 'message sent successfully
'Try
' Integer.TryParse(myWebResponse.Headers("Number-Of-MT-PDU"), num_MT_PDU)
'Catch ex As Exception
'End Try
Else
SendStatus = 2 'message processed but not sent successfully
End If
Catch e As WebException
If (e.Status = WebExceptionStatus.ProtocolError) Then
Dim response As WebResponse = e.Response
Using (response)
Dim httpResponse As HttpWebResponse = CType(response, HttpWebResponse)
statusCode = httpResponse.StatusCode
Try
myStreamReader = New StreamReader(response.GetResponseStream())
Using (myStreamReader)
ResponseText = myStreamReader.ReadToEnd & "Status Description = " & httpResponse.StatusDescription ' HttpWebResponse.StatusDescription
End Using
Catch ex As Exception
'Logger.LogError(Me, ex)
ex.Dump("Exception")
End Try
End Using
End If
End Try
ResponseText.Dump("ResponseText")
I have also confirmed the above code (with the inferred As clauses added and converting the .Dump calls to Console.WriteLine) works in .NET 2.0 with VB8.
Note that the key is that even though the act of GetResponseStream() throws a .NET WebException, the HttpWebResponse is actually passed to the WebException object, so when in the Catch, you do a new GetResponseStream() on the WebException.Response object.
Below, very similar code for when in the Catch of the initial GetResponseStream()
Try
OriginalResponseStream = GetResponseStream(OriginalHTTPWebResponse)
Catch wex as WebException
Dim response As WebResponse = wex.Response
Dim statusCode As HttpStatusCode
Dim ResponseText As String
Dim httpResponse As HttpWebResponse = CType(response, HttpWebResponse)
statusCode = httpResponse.StatusCode
Try
Dim myStreamReader As New StreamReader(response.GetResponseStream())
Using (myStreamReader)
ResponseText = myStreamReader.ReadToEnd
Process(ResponseText) '<===as in whatever you need to do with the response
End Using
Catch ex As Exception
HandleIt(ex.Message) '<===as in whatever you want to do if Exception during the above
End Try
End Try