Utilize a 3rd Party API in VB.NET
I know this may be a simple answer or at least to most people but I'm not getting anywhere when attempting this on my own or through the various resources I have looked up. I'm pretty sure I'm just physically searching the wrong terminology or phrases to get my answers.
Here is my issue:
I am using ASP/VB.NET to build this integration. Visual S开发者_如何转开发tudio 2008. I previously had asked a question on API Integration on here and utilized that information to successfully perform the next steps. As this API is written differently I may not be grasping the appropriate functions correctly.
A company has given me several addresses for .ASMX portals. I add these into my service references. I can successfully see objects in my object browser and see all the procedures, etc., but further from here I'm unable to correctly use any objects. I attempted to replicate the integration based on my last API question and advice: Connecting to an API offered by a Company. When I'm attempting to use an object or simply relate a field to something on my script I continually get an error.
For example:
Dim A as New API.AddFunction
A.AccountNo = "123"
When running the page: Object reference not set to an instance of an object.
Here is only one .ASMX I was given. I cannot release any more, until I receive permission to do so from the provider, I apologize.
http://cert1.tpayment.com:14935/Terminal.asmx
Anyhow, the root of my question is really how do I correctly interface with this API? What information do responders require so I can clarify this question more? Understandably you cannot guess at the problems until more information is shared. Is there a primer I can read through online?
The word you are looking for is XML web service or SOAP web service. This will help you with getting further with finding information on how to use such API. Specifically, your task is to "consume an XML web service using ASP.NET".
By having a quick look at the sample web service you provided it looks more than manageable. However, one thing you have to always remember that a web service operates on a "request - response" basis even though it is somewhat concealed when you add it as service reference to your project and it starts to look like just ordinary classes with methods and properties.
Here's a quick example to get you started. Let's assume you have a button "Button1" and a label "Label1" on your aspx page and that you added your example service as service reference with the namespace "Terminal". Here's a little server-side code for the click event of the button:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim SoapClient As New Terminal.TerminalSoapClient
Dim Credentials As New Terminal.OnBoardCredentials
Dim DataStructure As New Terminal.TerminalDataStructure
Dim ErrorMessages() As Terminal.OBAErrorMessage
Dim result As Boolean
Credentials.UserName = "username"
Credentials.PassWord = "password"
DataStructure.AccountNo = "123"
DataStructure.TerminalID = "123"
result = SoapClient.Add(Credentials, DataStructure, ErrorMessages)
Label1.Text = result.ToString & "; " & ErrorMessages(0).ErrorCode & " : " & ErrorMessages(0).ErrorMessage
End Sub
As you can see to start working with the web service you need a SOAP client. The client will expose the methods of the service such as "AddTerminal" in the example service. Then you need to put together the arguments for that operation, which in the case of AddTerminal are "OnBoardCredentials", "terminal" (represented by the generic TerminalDataStructure class) and "ErrorMessage). Also note that this particular operation requires an array of ErrorMessages since the web service can return more than one in a single response. Once you have specified the properties of each argument you are ready to call the operation (i.e. send the request) and use the returned value(s) (i.e. the response). Technically, the response of this particular operation contains an overall result of the Add operation as boolean as well as error messages if any. However, only the overall result is returned by the TerminalSoapClient.Add operation whilst the error messages are added back to your array of OBAErrorMessage.
Lastly, for demo purposes I present the results back to label on the page. When I try it I get
False; 13000 : Error during Login
since I don't have valid credentials to log in to the service.
Though very crude I hope this example helps to get you started.
精彩评论