pvg1975
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
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)
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?
ASKER
Maybe because I have no idea :(
I added the reference, but I just cant make it work.
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:
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:
This object is what you will use to communicate with the service. It appears your service has one method:
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:
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:
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:
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 = )
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()
This object is what you will use to communicate with the service. It appears your service has one method:
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:
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)
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:
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.
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.
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.
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
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
untitled.PNG
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.linkSearchServiceV2 PortType' 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 linkSearchServiceV2PortTyp eClient : System.ServiceModel.Client Base<LINKS .linkSearc hServiceV2 PortType>, LINKS.linkSearchServiceV2P ortType {
Line 553:
Line 554: public linkSearchServiceV2PortTyp eClient() {
Line 555: }
Line 556:
I made it work, but this is the error I get:
Could not find default endpoint element that references contract 'LINKS.linkSearchServiceV2
Source Error:
Line 552: public partial class linkSearchServiceV2PortTyp
Line 553:
Line 554: public linkSearchServiceV2PortTyp
Line 555: }
Line 556:
ASKER
I got it, I forgot to upload web.config to the server :P
Thanks!!!!!!!!
Thanks!!!!!!!!
Awesome! Glad to help = )
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...
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
https://www.experts-exchange.com/questions/27247031/Problem-with-webservice-part-2.html
Thanks!!!
Paula