-
Sep 7th, 2018, 08:45 AM
#1
Thread Starter
Addicted Member
Very new to webservice calls. Need some guidence.
I am working with a company called BPM Online. They are a CRM provider in the cloud. They have some information (data of ours) that I want to get and put into SQL. They are not being very good with me and that may be because I don't know much in this area. I am wondering how to do this. It is my understanding I can do a web service call to a web service to get this information. It is also my understanding they would have to setup the web service on there side for me to call to. Is this correct? I am not dealing with a guy that understands tech too much so talking to him has been difficult. Any information would be appreciated.
Thanks
-
Sep 7th, 2018, 10:32 AM
#2
Re: Very new to webservice calls. Need some guidence.
Yes - that is basically it. They give you a URL to POST some XML or JSON to usually - and it responds back with some XML or JSON.
Here I am talking to TXTSIGNAL with code
Code:
Function sendRequest(actionstring As String) As XDocument
Dim postXml As XDocument = <?xml version='1.0'?>
<txtsig_request version='2.0'>
<credentials>
<api_username><%= g_api_username %></api_username>
<api_password><%= g_api_password %></api_password>
<client_id><%= g_client_id %></client_id>
</credentials>
<%= XElement.Parse(actionstring) %>
</txtsig_request>
Dim postString As String = postXml.Declaration.ToString & postXml.ToString
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(g_url), HttpWebRequest)
request.Method = "POST"
request.AllowWriteStreamBuffering = False
request.PreAuthenticate = True
request.ContentType = "application/xml"
request.ContentLength = postString.Length
request.KeepAlive = True
Dim outputstream As Stream = request.GetRequestStream()
Dim postBytes As Byte() = Encoding.ASCII.GetBytes(postString)
outputstream.Write(postBytes, 0, postBytes.Length)
Dim response As WebResponse = request.GetResponse()
Dim datastream As Stream = response.GetResponseStream()
Dim xd As XDocument = XDocument.Load(datastream, LoadOptions.None)
outputstream.Close()
response.Close()
Return xd
End Function
And that function is used like this:
Code:
Function callTxtSignal(callWhat As String, sw As StreamWriter) As Boolean
Dim rtnStatus As Boolean = False
Dim actionstring = ""
Select Case callWhat.ToLower
Case "list"
sw.WriteLine("...list...")
actionstring = "<action type=""list_contacts""></action>"
sw.WriteLine(sendRequest(actionstring))
rtnStatus = True
Case "team"
sw.WriteLine("...team...")
Dim actionXml As XElement
actionXml = <action type="create_team">
<param name="team_name">CD5-for-application</param>
<param name="group">Members</param>
</action>
Dim xd As XDocument = sendRequest(actionXml.ToString)
Dim elems = xd.<txtsig_response>.<content>.<item>
Dim errorNode = From el In elems.<element> Where el.@name = "error_text"
Dim errorText As String = ""
Try
errorText = errorNode(0).Value
Catch ex As Exception
errorText = ""
End Try
If errorText <> "" Then
sw.WriteLine("** API Error ** " & "team")
sw.WriteLine(errorText)
sw.WriteLine(actionXml.ToString)
End If
Case "listteams"
Dim actionXml As XElement
actionXml = <action type="list_teams">
<param name="group">Members</param>
</action>
sw.WriteLine(sendRequest(actionXml.ToString))
rtnStatus = True
Case "queryteams"
Dim actionXml As XElement
actionXml = <action type="query_team">
<param name="group">Members</param>
<param name="team_name">CD3-for-application</param>
</action>
sw.WriteLine(sendRequest(actionXml.ToString))
rtnStatus = True
-
Sep 10th, 2018, 09:26 AM
#3
Thread Starter
Addicted Member
Re: Very new to webservice calls. Need some guidence.
Thank you!!! Just got to this today. This is pretty new to me. I will have to bone up on this xdocument stuff.
-
Sep 10th, 2018, 12:00 PM
#4
Thread Starter
Addicted Member
Re: Very new to webservice calls. Need some guidence.
Code:
Option Infer On
Imports System
Imports System.IO
Imports System.Net
Imports System.Web
Namespace RequestAuthentification
' Auxiliary class for de-serialization of the JSON object from the HTTP reply.
Friend Class ResponseStatus
Public Property Code() As Integer
Public Property Message() As String
Public Property Exception() As Object
Public Property PasswordChangeUrl() As Object
Public Property RedirectUrl() As Object
End Class
' Primary class of the application.
Friend Class Program
' HTTP address of the application.
Private Const baseUri As String = "http://appliedrobotics.bpmonline.com"
' Container for Cookie authentication in bpm'online. Must be used in subsequent requests.
' This is the most important resulting object.
' The rest of the functions in this example are developed for implementation of its properties.
Public Shared AuthCookie As New CookieContainer()
' A request string to the "Login" method of the "AuthService.svc" service.
Private Const authServiceUri As String = baseUri & "/ServiceModel/AuthService.svc/Login"
' Performs user authentication request.
Public Shared Function TryLogin(ByVal userName As String, ByVal userPassword As String) As Boolean
' Creating an instance of the authentication service request.
Dim authRequest = TryCast(HttpWebRequest.Create(authServiceUri), HttpWebRequest)
' Defining the request's method.
authRequest.Method = "POST"
' Defining the request's content type.
authRequest.ContentType = "application/json"
' Enabling the use of cookie in the request.
authRequest.CookieContainer = AuthCookie
' Placing user credentials to the request.
Using requestStream = authRequest.GetRequestStream()
Using writer = New StreamWriter(requestStream)
writer.Write("{""UserName"":""" & "User" & """,""UserPassword"":""" & "Pass" & """}")
End Using
End Using
' Auxiliary object where the HTTP reply data will be de-serialized.
Dim status As ResponseStatus = Nothing
' Getting a reply from the server. If the authentication is successful, cookie will be placed to the AuthCookie property.
' These cookies can be used for subsequent requests.
Using response = CType(authRequest.GetResponse(), HttpWebResponse)
Using reader = New StreamReader(response.GetResponseStream())
' De-serialization of the HTTP reply to an auxiliary object.
Dim responseText As String = reader.ReadToEnd()
status = (New System.Web.Script.Serialization.JavaScriptSerializer()).Deserialize(Of ResponseStatus)(responseText)
End Using
End Using
' Checking authentication status.
If status IsNot Nothing Then
' Authentication is successful.
If status.Code = 0 Then
Return True
End If
' Authentication is unsuccessful.
Console.WriteLine(status.Message)
End If
Return False
End Function
' Application login method.
Shared Sub Main(ByVal args() As String)
' Calling authentication method.
Console.WriteLine("Is authentication successful?: {0}", TryLogin("User 1", "User 1"))
Console.WriteLine("Press ENTER to close...")
Console.ReadLine()
End Sub
End Class
End Namespace
-
Sep 10th, 2018, 12:03 PM
#5
Thread Starter
Addicted Member
Re: Very new to webservice calls. Need some guidence.
The above code is erroring out with the status = (New System.Web.Script.Serialization.JavaScriptSerializer()).Deserialize(Of ResponseStatus)(responseText) line.
Last edited by BSWhipp; Sep 10th, 2018 at 12:05 PM.
Reason: changed some text
-
Sep 11th, 2018, 09:25 AM
#6
Re: Very new to webservice calls. Need some guidence.
Originally Posted by BSWhipp
The above code is erroring out with the status = (New System.Web.Script.Serialization.JavaScriptSerializer()).Deserialize(Of ResponseStatus)(responseText) line.
I'll give the standard jmc answer of, "If only we knew what the error was..." to save szlamany the time.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|