How do I solve this error? - asp.net

Consider this code:
Dim strUrl As String = "http://www.txtguru.in/imobile/api.php?username=username&password=password&source=senderid&dmobile=123456789012&message=message"
Dim request As WebRequest = HttpWebRequest.Create(strUrl)
Dim response As HttpWebResponse = DirectCast(request.EndGetResponse(), HttpWebResponse)
Dim s As Stream = DirectCast(response.GetResponseStream(), Stream)
Dim readStream As New StreamReader(s)
Dim dataString As String = readStream.ReadToEnd()
response.Close()
s.Close()
readStream.Close()
I get an error on request.EndGetResponse() and the error is:
Argument not specified for parameter 'asyncResult' of 'Public Overridable Function EndGetResponse(asyncResult As System.IAsyncResult) As System.Net.WebResponse'.
How can I fix it?

Did you mean to use the asynchronous version:
Dim response As HttpWebResponse = DirectCast(request.EndGetResponse(), HttpWebResponse)
Try using the synchronous one instead:
Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)

Try changing -
Dim response As HttpWebResponse = DirectCast(request.EndGetResponse(), HttpWebResponse)
to
Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)

Related

Convert cURL to ASP.Net

I'm looking for a way to convert the following cURL to ASP.Net.
curl -F f=#example.pdf "https://pdftables.com/api?key=ZZZ999&format=xml"
I've used the following function extensively to get virtually any URL/content, but I'm lost on how to include a file located on the hosted web server.
Public Shared Function GetRemoteURL(getURL As String) As String
Dim objReq As HttpWebRequest
Dim objRsp As HttpWebResponse = Nothing
Dim objRdr As StreamReader
Dim objRes As String = ""
Try
objReq = DirectCast(WebRequest.Create(getURL), HttpWebRequest)
objRsp = DirectCast(objReq.GetResponse(), HttpWebResponse)
objRdr = New StreamReader(objRsp.GetResponseStream())
objRes = objRdr.ReadToEnd()
Catch
objRes = ""
Finally
If Not objRsp Is Nothing Then objRsp.Close()
End Try
Return objRes.ToString()
End Function
Any advice/direction would be deeply appreciated.
John
You'll have to set the request body, something like:
Public Function GetRemoteURL(getURL As String) As String
Dim objReq As HttpWebRequest
Dim objRsp As HttpWebResponse = Nothing
Dim objRdr As StreamReader
Dim objRes As String = ""
Try
objReq = DirectCast(WebRequest.Create(getURL), HttpWebRequest)
objReq.Method = "POST"
Dim postData = "f=#example.pdf"
Dim encoding As New ASCIIEncoding()
Dim bytes = encoding.GetBytes(postData)
objReq.ContentLength = bytes.Length
Dim stream = objReq.GetRequestStream()
stream.Write(bytes, 0, bytes.Length)
objRsp = DirectCast(objReq.GetResponse(), HttpWebResponse)
objRdr = New StreamReader(objRsp.GetResponseStream())
objRes = objRdr.ReadToEnd()
Catch
objRes = ""
Finally
If Not objRsp Is Nothing Then objRsp.Close()
End Try
Return objRes.ToString()
End Function
note: i didn't test this so it might not directly work.

System.Net.WebException' occurred in System.dll but was not handled in user code

Try
Dim url = "something"
Dim request = WebRequest.Create(url)
request.Credentials = CredentialCache.DefaultCredentials
Dim httpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
Dim dataStream = httpWebResponse.GetResponseStream()
If dataStream Is Nothing Then
Return ""
End If
Dim reader = New StreamReader(dataStream)
Return reader.ReadToEnd()
Catch ex As WebException
Return ""
End Try
Above code is working in LocalHost but in server it's not working and throwing an Exception.
System.Net.WebException' occurred in System.dll but was not handled
in user code
You're re-throwing the exception after the Catch.
Remove Throw.
Try
Dim url = "something"
Dim request = WebRequest.Create(url)
request.Credentials = CredentialCache.DefaultCredentials
Dim httpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
Dim dataStream = httpWebResponse.GetResponseStream()
If dataStream Is Nothing Then
Return ""
End If
Dim reader = New StreamReader(dataStream)
Return reader.ReadToEnd()
Catch ex As WebException
Return ""
End Try

Soap Request : 500 - Internal Server Error

This is my code it is in vb.net . I did'nt get response from soap request , i have no idea what is the wrong with this code , its showing 500 Internal Server Error.
Dim webRequest__1 As WebRequest = WebRequest.Create("https://login.twinfield.com/webservices/session.asmx?wsdl")
Dim httpRequest As HttpWebRequest = DirectCast(webRequest__1, HttpWebRequest)
httpRequest.Method = "POST"
httpRequest.ContentType = "application/soap+xml;charset=UTF-8;action='http://www.twinfield.com/Logon'"
httpRequest.Host = "login.twinfield.com"
httpRequest.Headers.Add("SOAPAction:https://login.twinfield.com/webservices/session.asmx")
httpRequest.ProtocolVersion = HttpVersion.Version11
httpRequest.Credentials = CredentialCache.DefaultCredentials
Dim requestStream As Stream = httpRequest.GetRequestStream()
'Create Stream and Complete Request
Dim streamWriter As New StreamWriter(requestStream, Encoding.ASCII)
' <soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:twin='http://www.twinfield.com/'>
Dim soapRequest As New StringBuilder("<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:twin='http://www.twinfield.com/'><soap:Header/><soap:Body><twin:Logon><twin:user>dgf</twin:user> <twin:password>cfg</twin:password><twin:organisation>dfd</twin:organisation></twin:Logon></soap:Body></soap:Envelope>")
'soapRequest.Append(" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" ")
'soapRequest.Append("xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""><soap:Body>")
'soapRequest.Append("<GetMyName xmlns=""http://tempuri.org/""><name>Sam</name></GetMyName>")
'soapRequest.Append("</soap:Body></soap:Envelope>")
streamWriter.Write(soapRequest.ToString())
streamWriter.Close()
'Get the Response
Dim htttpresponse As HttpWebResponse = CType(httpRequest.GetResponse(), HttpWebResponse)
'Dim wr As HttpWebResponse = CType(httpRequest.GetResponse(), HttpWebResponse)
'DirectCast(httpRequest.GetResponse(), HttpWebResponse)
Dim srd As New StreamReader(htttpresponse.GetResponseStream())
Dim resulXmlFromWebService As String = srd.ReadToEnd()
'Return resulXmlFromWebService
To call a soap web service, I think it is better to add the service url as a web reference (i.e. name as twinfield) in visual studio. Then you can use the service as a function as below.
twinfield.Session s = new twinfield.Session();
twinfield.LogonAction ac;
string cl = "";
twinfield.LogonResult k = s.Logon("dsfa", "Sadfsa", "sdfas", out ac, out cl);

HttpWebRequests Returning error from Youtube

I cannot seem to understand what I'm doing wrong here.
My code below keeps returning a 400 code from youtube....
If Not Page.Request.QueryString("code") Is Nothing Then
Dim code As String = "code=" & Page.Request.QueryString("code") & "&client_id=myclientid&client_secret=mysecret&redirect_uri=http://localhost:61163/Testing/YoutubeAPI.aspx&grant_type=authorization_code"
Dim request As HttpWebRequest = WebRequest.Create("https://accounts.google.com/o/oauth2/token")
Dim byteData As Byte() = Encoding.UTF8.GetBytes(code)
With request
.Method = "POST"
.ContentType = "application/x-www-form-urlencoded"
.ContentLength = byteData.Length
End With
Dim requestStream As Stream = request.GetRequestStream()
requestStream.Write(byteData, 0, byteData.Length)
requestStream.Close()
Dim WebResponse As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
Dim responseStream As Stream = WebResponse.GetResponseStream()
Dim sb As StringBuilder = New StringBuilder
Using reader As New StreamReader(responseStream, System.Text.Encoding.UTF8)
Dim line As String = reader.ReadLine()
If Not line Is Nothing Then
sb.Append(line)
End If
End Using
End If
the error occurs at request.GetRequestStream()... the best I can figure out at this stage is that google doesn't like what i'm asking for but cannot seem to find out why?
(My client ID and secret have been swopped incidentally....)
well am not sure if the etiquette is correct here in answering my own question but you may find it useful nonetheless.
The answer lay in the URL encoding of content string...
so now slightly amended the awesomeness of OAuth2 is unleashed and I can get pretty much whatever I want from the youtube API :) (although NO thanks to the black hole rabbit-hole of google documentation...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.Request.QueryString("code") Is Nothing Then
Dim Token As String = CodeTrade("code=" & Server.UrlEncode(Page.Request.QueryString("code")) & "&redirect_uri=" & Server.UrlEncode("http://localhost:61163/Testing/YoutubeAPI.aspx") & "&client_id=xxx=&client_secret=xxx&grant_type=authorization_code")
'now i CAN do something with the magical and elusive access_token from this point forward....
End If
End Sub
Public Shared Function CodeTrade(code As String) As String
Dim apiResponse As String
Dim postData As String = code
Dim request As HttpWebRequest = DirectCast(WebRequest.Create("https://accounts.google.com/o/oauth2/token"), HttpWebRequest)
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
Dim dataStream As Stream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim response As WebResponse = request.GetResponse()
apiResponse = DirectCast(response, HttpWebResponse).StatusDescription.ToString()
dataStream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
reader.Close()
dataStream.Close()
response.Close()
Return responseFromServer
End Function

HTTP post request failed from sending on server

I'm trying to post these variable threw an engine which gives back a responce but while testing it localy on VStudio everything is normal. When i upload it on the server the post request are not precessed.
Dim webStream As Stream
Dim s As HttpWebRequest`enter code here`
Dim res As HttpWebResponse
Dim webResponse = ""
Dim enc As UTF8Encoding
Dim postdata As String
Dim postdatabytes As Byte()
s = HttpWebRequest.Create("http://private.pin-pay.com:3902/")
enc = New System.Text.UTF8Encoding()
postdata = "PN=961" & ddlPhone.SelectedValue & txtMobilePhone.Text & "&TI=" & TransactionID.Text
postdatabytes = enc.GetBytes(postdata)
s.Method = "POST"
s.ContentType = "application/x-www-form-urlencoded"
s.ContentLength = postdatabytes.Length
Using stream = s.GetRequestStream()
stream.Write(postdatabytes, 0, postdatabytes.Length)
End Using
res = s.GetResponse()
webStream = res.GetResponseStream()
Dim webStreamReader As New StreamReader(webStream)
While webStreamReader.Peek >= 0
webResponse = webStreamReader.ReadToEnd()
End While

Resources