Submit Form POST using VB .NET - vb.net

I have searched a solution to my problem extensively, and while I found answers that seemed to have worked for others, I am just having a real hard time figuring this out. But I feel I am very close.
I am trying to make an Rest API call to an online application called Zoho Creator. I am trying to implement the Add Record call. The example they give is using an HTML form with a submit button. But I need to Add Records from a VB .NET desktop application. I have tried both WebClient and WebRequest, but I am unsuccessful in those attempts. But I have been successful using these methods with other API calls and other APIs, it's just this Add Records one that is giving me trouble.
One of the required parameters is an authtoken, which for security reasons I replaced with "xxxxxxxxxx". Here is an html form that I created and when I use it, it created the record successfully thru the API, it's just adding a single record with the single field value for "TicketID".
<!DOCTYPE html>
<html>
<body>
<form method="POST" action="https://creator.zoho.com/api/max1stdirectcom/xml/service-orders/form/Ticket_form/record/add">
<input type="hidden" name="authtoken" value="xxxxxxxxxx"/>
<input type="hidden" name="scope" id="scope" value="creatorapi"/>
<input type="text" name="TicketID" value="123"/>
<input type="submit" value="Add Record"/>
</form>
<body>
So, the above works perfectly fine. now, here is my VB .NET code trying to replicate the same result using WebRequest:
Protected Sub PostTo(sTicketID As String)
Dim url As String = "https://creator.zoho.com/api/max1stdirectcom/xml/service-orders/form/Ticket_form/record/add"
Dim request As WebRequest = WebRequest.Create(url)
request.Method = "POST"
' Create POST data and convert it to a byte array.
Dim postData As String = "?authtoken=" & "xxxxxxxxxx" & "?scope=creatorapi" & "?TicketID=" & sTicketID
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
' Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded"
' Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length
' Get the request stream.
Dim dataStream As Stream = request.GetRequestStream()
' Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length)
' Close the Stream object.
dataStream.Close()
' Get the response.
Dim response As WebResponse = request.GetResponse()
' Display the status.
Debug.WriteLine(CType(response, HttpWebResponse).StatusDescription)
' Get the stream containing content returned by the server.
dataStream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
Debug.WriteLine(responseFromServer)
' Clean up the streams.
reader.Close()
dataStream.Close()
response.Close()
End Sub
The response I get from the call using the above VB .Net code is:
<response>
<errorlist>
<error>
<code>2899</code>
<message><![CDATA[Permission Denied To Add Record(s).]]></message>
</error>
</errorlist>
</response>
So it is obviously making good communication with the API on some level. I am using the correct AuthToken, so not sure why it is rejecting the adding of the record. I am passing the same exact "credentials" as the basic form POST, but getting different result.
Any recommendations for me to try?

Below is a working code in VB .Net
Please check and add the missing implementation in your code.
Private Sub HTTPRestPOST (ByVal JsonInputStr As String, ByVal POSTUri As String)
'Make a request to the POST URI
Dim RestPOSTRequest As HttpWebRequest = HttpWebRequest.Create(POSTUri)
'Convert the JSON Input to Bytes through UTF8 Encoding
Dim JsonEncoding As New UTF8Encoding()
Dim JsonBytes As Byte() = JsonEncoding.GetBytes(JsonInputStr)
'Setting the request parameters
RestPOSTRequest.Method = "POST"
RestPOSTRequest.ContentType = "application/json"
RestPOSTRequest.ContentLength = JsonBytes.Length
'Add any other Headers for the URI
RestPOSTRequest.Headers.Add("username", "kalyan_nakka")
RestPOSTRequest.Headers.Add("password", "********")
RestPOSTRequest.Headers.Add("urikey", "MAIJHDAS54ADAJQA35IJHA784R98AJN")
'Create the Input Stream for the URI
Using RestPOSTRequestStream As Stream = RestPOSTRequest.GetRequestStream()
'Write the Input JSON data into the Stream
RestPOSTRequestStream.Write(JsonBytes, 0, JsonBytes.Length)
'Response from the URI
Dim RestPOSTResponse = RestPOSTRequest.GetResponse()
'Create Stream for the response
Using RestPOSTResponseStream As Stream = RestPOSTResponse .GetResponseStream()
'Create a Reader for the Response Stream
Using RestPOSTResponseStreamReader As New StreamReader(RestPOSTResponseStream)
Dim ResponseData = RestPOSTResponseStreamReader.ReadToEnd()
'Later utilize "ResponseData" variable as per your requirement
'Close the Reader
RestPOSTResponseStreamReader.Close()
End Using
RestPOSTResponseStream.Close()
End Using
RestPOSTRequestStream.Close()
End Using
End Sub

Related

how can show captcha image in pictureBox

i have webbrowser control to open website
I want to get captcha image that appears on this web page to show in pictureBox.....
<img src="/alpha/captcha.php?1393609547" width="150" height="25" title="Click for another image" alt="CAPTCHA" id="asc" />
An example of this
Thanks for your help
First, I see that the image src URL is a relative URL. That means you need to make it an absolute by doing something like this:
Dim absoluteURL As String = "domain of captcha" & captchaURL
Then, you need to download the image into Bytes by doing something like this:
Dim PictureBytes As Byte()
' Convert String to a Uri
Dim address As New Uri(absoluteURL, UriKind.Absolute)
' Create the web request
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(address), HttpWebRequest)
' Set type to GET
request.Method = "GET"
Try
' Get response
Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
' Get the response stream into a reader
Dim stream As Stream = response.GetResponseStream
Dim reader New BinaryReader(stream)
PictureBytes = reader.ReadBytes(stream.Length)
Finally
If Not response Is Nothing Then response.Close()
End Try
Finally, you will need to convert the bytes to a bitmap and put that bitmap in a picture box
Dim Captcha As New Bitmap(new MemoryStream(PictureBytes));
pictureBox.Image = Captcha
If you encounter any problems, just comment on this post. If this code works, please consider marking this correct and up-voting. Please note, up-voting or marking an answer correct DOES NOT subtract reputation.

Windows Phone 8 Unsupported API cannot be used by a background agent

I have built a Windows Phone 8 app which compiles and runs without any problems. However, when I run the built-in VS Store Test Kit, it brings back LOTS of errors under XAP package requirements, formatted as such:
[ERROR]: Unsupported API cannot be used by a background agent.
Assembly Microsoft.Phone.Controls.Toolkit.dll was trying to use
Microsoft.Phone.Shell.ApplicationBarIconButton.
I'm getting errors for my project dll ([projectname].dll) and the Toolkit dll. It seems to be bringing back everything, even references I'm not using (such as add_OrientationChanged).
Now I've had a look through the Unsupported APIs listed by Microsoft, and I'm not using any of them. At the bottom of the page is a list of "noteworthy APIs", the one of which I;m using is HTTPWebRequest to connect to a web service. I'm assuming this is where the problem lies, as I use this throughout the app.
Here's an example of what I'm doing with HTTPWebRequest:
Public Sub LoadRequest()
Dim request As HttpWebRequest = CType(WebRequest.Create(_ServiceURL), HttpWebRequest)
request.ContentType = "text/xml;charset=utf-8"
request.Method = "POST"
request.Headers("SOAPAction") = "<web service address>"
Start the asynchronous operation.
Dim result As IAsyncResult = _
CType(request.BeginGetRequestStream(AddressOf GetRequestsStreamCallback, request), _
IAsyncResult)
End Sub
Public Sub GetRequestsStreamCallback(ByVal asyncResult As IAsyncResult)
Dim request As HttpWebRequest = CType(asyncResult.AsyncState, HttpWebRequest)
Dim soapBody As System.Xml.Linq.XDocument = System.Xml.Linq.XDocument.Parse( _
"<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' " & _
"xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Header><[SOAP Header Here]></soap:Header>" & _
"<soap:Body><[SOAP Body Here]></soap:Body></soap:Envelope>")
'End the operation
Dim postStream As Stream = request.EndGetRequestStream(asyncResult)
'Convert the string into byte array.
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(soapBody.ToString())
'Write to the stream.
postStream.Write(byteArray, 0, soapBody.ToString().Length)
postStream.Close()
Dispatcher.BeginInvoke(Sub()
Dim result As IAsyncResult = _
CType(request.BeginGetResponse(AddressOf GetRequestsResponseCallback, request), IAsyncResult)
End Sub)
End Sub
Public Sub GetRequestsResponseCallback(ByVal Result As IAsyncResult)
'Get the response
Dim request As HttpWebRequest = CType(Result.AsyncState, HttpWebRequest)
Dim response As HttpWebResponse = CType(request.EndGetResponse(Result), HttpWebResponse)
Dim streamResponse As Stream = response.GetResponseStream()
Dim streamRead As New StreamReader(streamResponse)
Dim responseString As String = streamRead.ReadToEnd()
streamResponse.Close()
streamRead.Close()
'Take action on the response
Dim Xml As XDocument = XDocument.Parse(responseString)
Dim ns As XNamespace = "<Service Address>"
etc...
End Sub
I then proceed to pull apart the XML response for the data I need for the app. Any ideas as to what the test kit isn't happy about here? I'm assuming that as it doesn't pass validation at this stage, it's likely to be rejected from the store.
Thanks in advance for any ideas.
The problem was an existing background agent I was unaware of (I took over the project from another developer). There was a reference in the WMAppManifest.xml file, which was apparently part of an early test using the Wallet, which is no longer required. Removing this reference fixed the problem.
<ExtendedTask Name="BackgroundTask">
<BackgroundServiceAgent Specifier="WalletAgent" Name="WalletAgent" Source="<ProjectName>" Type="<ProjectName>.WPWalletAgent" />
</ExtendedTask>

How to catch the post data sent to the redirect URL (VB 2010)

I am trying to login to one web page via my Windows Form application (VB 2010) and get some response, but I don't know how to do it.
The server service is described here:
https://api.developer.betfair.com/services/webapps/docs/display/1smk3cen4v3lu3yomq5qye0ni/Interactive+Login+from+a+Desktop+Application
On beginning I tried to insert WebBrowser Control to my form, but I read many articles, also here, that using BeforeNavigate2 event is an old way.
I would like to do this:
Send HTTP request to webserver including username and password
Catch the post data sent to the redirect URL
Read the POST request body an get loginStatus and productToken (SSOID)
This is my code:
Private Sub getPOST()
' Create a request for the URL
Dim request As WebRequest = _
WebRequest.Create("https://identitysso.betfair.com/view/login?product=82&url=https://www.betfair.com&username=abc&password=abc")
request.Method = "POST"
' Get the response
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
' Get the stream containing content returned by the server.
Dim dataStream As Stream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
RichTextBox1.Text = responseFromServer
' Cleanup the streams and the response.
reader.Close()
dataStream.Close()
response.Close()
End Sub
But this code returns HTML code of page where are not loginStatus and SSOID.
I examined this page and discovered there are a few problems with your code:
First you are posting to the wrong URL because the www.betfair.com/view/login form has action="/api/login", not "/view/login". Second, you are passing your username/password in the QueryString rather than in POSTDATA... this form uses method=post. Third, you are not passing all required fields. This code snippet should remedy these described problems, as well as answer your question about obtaining the redirect URL:
Sub TestWebClient()
' Create a request for the URL
' You were using the wrong URL & passing the values improperly
' I also changed this to HttpWebRequest, used to be WebRequest.
Dim request As HttpWebRequest = WebRequest.Create("https://identitysso.betfair.com/api/login") '
request.Method = "POST"
' New code added
request.ContentType = "application/x-www-form-urlencoded"
' New code added, this prevents the following of the 302 redirect in the Location header
request.AllowAutoRedirect = False
' New code added, this sends the values as POSTDATA, which is contained inside the HTTP POST request rather than in the URL
Using writer As New StreamWriter(request.GetRequestStream)
writer.Write("username=abc")
writer.Write("&password=abc")
writer.Write("&login=true")
writer.Write("&redirectMethod=POST")
writer.Write("&product=82")
writer.Write("&url=https://www.betfair.com/")
writer.Write("&ioBlackBox=")
End Using
' Get the response
' This will print the Location header (aka "Redirect URL") on the screen for you
' Make decisions as needed.
Dim response As HttpWebResponse = request.GetResponse()
Console.WriteLine("HTTP RESPONSE HEADERS:")
For Each item In response.Headers
Console.WriteLine(item & "=" & response.Headers(item))
Next
Console.ReadLine()
End Sub

Empty response HTTPWebRequest: Windows Phone 8

I am making a Windows Phone app and I am trying to get JSON data from a URL. The user needs to be logged into the website (which hosts the JSON data) in order to get JSON data and I cannot use the Web Browser control to display the data and then extract the string since the browser doesn't recognize it (for some weird reason) and asks to search for an app on Store which can handle that JSON file type. (If I open the URL in desktop browser on my Windows PC, I can see the raw JSON data). I can't use normal HTTPWebRequest or WebClient as to get JSON data the login cookies needs to be set (the JSON data is user-specific) and I can't extract the cookies from Web browser control and use it with WebClient or HTTPWebRequest. So the best thing I can do is use a special internal instance of IWebRequestCreate that is used internally by the WebBrowser. By opening background HTTP requests with that class, the cookies get automatically set as if they were created/sent by the WebBrowser control. But my code is not returning the JSON data, I get blank response, as in the string resp is empty.
Below is the code:
Dim browser = New WebBrowser()
Dim brwhttp = GetType(WebRequestCreator).GetProperty("BrowserHttp")
Dim requestFactory = TryCast(brwhttp.GetValue(Browser, Nothing), IWebRequestCreate)
Dim uri = New Uri("http://api.quora.com/api/logged_in_user?fields=inbox,notifs,following,followers")
Dim req = requestFactory.Create(uri)
req.Method = "GET"
req.BeginGetResponse(New AsyncCallback(AddressOf request_Callback), req)
Private Sub request_Callback(asyncResult As IAsyncResult)
Dim webRequest As HttpWebRequest = DirectCast(asyncResult.AsyncState, HttpWebRequest)
Dim webResponse As HttpWebResponse = DirectCast(webRequest.EndGetResponse(asyncResult), HttpWebResponse)
Dim tempStream As New MemoryStream()
webResponse.GetResponseStream().CopyTo(tempStream)
Dim sr As New StreamReader(tempStream)
Dim resp As String = sr.ReadToEnd
End Sub
What's wrong?
I found that CopyTo can leave the Stream's pointer at the end of the buffer, you probably need to reset tempStream's pointer to the beginning before attempting to read it with the StreamReader, here's the code...
webResponse.GetResponseStream().CopyTo(tempStream);
tempStream.Seek(0, SeekOrigin.Begin);
Dim sr As New StreamReader(tempStream);

how to read xml from remote url in vb.net

i am working in a project in that i want to read some data from the remote url can any one help me how to do this function
You can use a WebRequest to retrieve the XML from the remote site; then you can parse the contents into an XmlDocument object.
' Create a WebRequest to the remote site
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://www.domain.com/fetch.xml")
' NB! Use the following line ONLY if the website is protected
request.Credentials = New System.Net.NetworkCredential("username", "password")
' Call the remote site, and parse the data in a response object
Dim response As System.Net.HttpWebResponse = request.GetResponse()
' Check if the response is OK (status code 200)
If response.StatusCode = System.Net.HttpStatusCode.OK Then
' Parse the contents from the response to a stream object
Dim stream As System.IO.Stream = response.GetResponseStream()
' Create a reader for the stream object
Dim reader As New System.IO.StreamReader(stream)
' Read from the stream object using the reader, put the contents in a string
Dim contents As String = reader.ReadToEnd()
' Create a new, empty XML document
Dim document As New System.Xml.XmlDocument()
' Load the contents into the XML document
document.LoadXml(contents)
' Now you have a XmlDocument object that contains the XML from the remote site, you can
' use the objects and methods in the System.Xml namespace to read the document
Else
' If the call to the remote site fails, you'll have to handle this. There can be many reasons, ie. the
' remote site does not respond (code 404) or your username and password were incorrect (code 401)
'
' See the codes in the System.Net.HttpStatusCode enumerator
Throw New Exception("Could not retrieve document from the URL, response code: " & response.StatusCode)
End If
Along with what #Jon Skeet said there's also the built-in WebClient:
Dim MyData As String
Try
Using WC As New System.Net.WebClient()
MyData = WC.DownloadString("http://www.example.com/text.xml")
End Using
Catch ex As Exception
'Error downloading
End Try
Have you tried using XDocument.Load or XmlDocument.Load?
If those don't do what you want, please give more details.
try this
http://www.codeproject.com/Tips/992109/Parsing-Reading-XML-from-URL-in-VB-NET
There are some useful examples provided with it. Hope this helps...