Link to home
Start Free TrialLog in
Avatar of pvg1975
pvg1975Flag for Argentina

asked on

API. What am I doing wrong?

Hello, I have the attached code for an API call.

The thing is that is not giving me any error, its just not showing anything. What am I doing wrong?

Thanks
Dim post_url As String
        post_url = "https://product.api.cj.com/wsdl/version2/productSearchServiceV2.wsdl"

        Dim post_values As New Dictionary(Of String, String)

        post_values.Add("developerKey", "xxxxxxxxxxxxxxxxx")
        post_values.Add("websiteid", "xxxxxxxxxx")
        post_values.Add("keywords", "GPS")


        Dim post_string As String = ""
        For Each field As KeyValuePair(Of String, String) In post_values
            post_string &= field.Key & "=" & HttpUtility.UrlEncode(field.Value) & "&"
        Next
        post_string = Left(post_string, Len(post_string) - 1)

        Dim objRequest As HttpWebRequest = CType(WebRequest.Create(post_url), HttpWebRequest)
        objRequest.Method = "POST"
        objRequest.ContentLength = post_string.Length
        objRequest.ContentType = "text/html"

        Dim myWriter As StreamWriter = Nothing
        myWriter = New StreamWriter(objRequest.GetRequestStream())
        myWriter.Write(post_string)
        myWriter.Close()


        Dim objResponse As HttpWebResponse = CType(objRequest.GetResponse(), HttpWebResponse)
        Dim responseStream As New StreamReader(objResponse.GetResponseStream())
        Dim post_response As String = responseStream.ReadToEnd()
        responseStream.Close()

        Response.Write(post_response)

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

Maybe it's just me, but why are you posting to the WSDL file? That file should describe your service. There should be another URL that points to one of the functions your service exposes. Have you tried using the "Add Service Reference" option of Visual Studio and pointing it at the WSDL file we can see in your code above?
Avatar of pvg1975

ASKER

Maybe because I have no idea :(

I added the reference, but I just cant make it work.
OK. When you go to add the reference, you should have had a window like this:

 User generated image
Notice I've filled in the Address field with your WSDL file's URL, and I subsequently pressed the "Go" button. Once I did that, the available services show under the "Services" pane. You don't need to select anything in that pane, even though it is selectable. Also notice at the bottom of the image, there is a text box denoting "Namespace". You can leave it at it's default value, but it might not make sense to you later. Whether you leave it or change it, just make note of what its value is because you will need that namespace in order to get to the generated classes.

Once you click "OK", you'll be back to your code. In code, you create an instance of the "proxy" class that was generated by the wizard. Something like:

Dim proxy As New ServiceReference1.productSearchServiceV2PortTypeClient()

Open in new window


This object is what you will use to communicate with the service. It appears your service has one method:

 User generated image
How do I know? Experience mainly. As you use web services more, you'll start to notice which methods always appear, and which are there specifically because of your service. The other methods are default methods generated whenever you use a service reference.

Your method takes one parameter and returns a result:

 User generated image
So you need to create an instance of the class that represents the input parameter and capture the response if it is important to you:

Dim proxy As New ServiceReference1.productSearchServiceV2PortTypeClient()
Dim req As New ServiceReference1.searchRequest()
Dim resp As ServiceReference1.searchResponse

resp = proxy.ServiceReference1_productSearchServiceV2PortType_search(req)

Open in new window


I've used the default constructor for the searchRequest class, but I'm guessing you'll need to use the overloaded version (i.e. the one that specifies, among other things, the developer key). Once you set up the parameters properly, you can execute the call and your response object should be ready to use:

 User generated image
It's always a good idea to wrap web service calls in a Try/Catch (even though I didn't show it). Try the above out and see if you get better results  = )
P.S.

I forgot to mention that outside of experience, you can always inspect the WSDL file to determine what functions are available to you via a web service. However, WSDL files can be quite intimidating to novices. Either method should get you by though.
P.P.S.

When looking back over my post, I noticed that there is another method "search" which is not one of the default methods. I believe the that method and the one I demonstrated above are interchangeable. You would need to make sure with the service provider, though.
Avatar of pvg1975

ASKER

Thans Kaufmed!

I did all you said, but I dont know how to pass the parameters developerKey and websiteId, that are required. Should I do that by code?

http://help.cj.com/en/web_services/web_services.htm#product_catalog_search_service_rest.htm
That goes back to what I said about using the overloaded version of the searchRequest class. Take a look:


untitled.PNG
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pvg1975

ASKER

I would give you 1,000,000 points for the quality of your response, but I cannot assign more than 500 :)

I made it work, but this is the error I get:

Could not find default endpoint element that references contract 'LINKS.linkSearchServiceV2PortType' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

Source Error:


Line 552:    public partial class linkSearchServiceV2PortTypeClient : System.ServiceModel.ClientBase<LINKS.linkSearchServiceV2PortType>, LINKS.linkSearchServiceV2PortType {
Line 553:        
Line 554:        public linkSearchServiceV2PortTypeClient() {
Line 555:        }
Line 556:
Avatar of pvg1975

ASKER

I got it, I forgot to upload web.config to the server :P

Thanks!!!!!!!!
Awesome! Glad to help  = )
Avatar of pvg1975

ASKER

Kauf, I have another question, but I will place it on a new question so you get more points. Allow me 2 minutes please. writing it now...
Avatar of pvg1975

ASKER

Here it is, Im sure its a simple answer for you based on what you have done on this post :)

https://www.experts-exchange.com/questions/27247031/Problem-with-webservice-part-2.html

Thanks!!!

Paula