Solved

VB and REST API

Posted on 2012-03-23
13
793 Views
Last Modified: 2012-03-23
Hello all,

I need to use this API:

http://help.cj.com/en/web_services/web_services.htm#Commission_Detail_Service.htm

to bring CJ comissions.... anyone know how to make this possible? I never worked with APIs.

Thanks!
0
Comment
Question by:pvg1975
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37757707
Since it's a REST API, you can more or less think of it like calling a web page. You could type the address in a web browser and (possibly) see the result of the call. There are examples of what the URLs should look like on the page you linked to. You just need to use the appropriate class to make web requests to their site.

It seems as though the provider may be serving requests via the GET method (what most web pages use). You'll need to check with the provider or their documentation to confirm, though.

You should be able to use either the HttpRequest class or the WebClient class to make requests to the provider's URLs.
0
 

Author Comment

by:pvg1975
ID: 37757855
Hi Kaufmed!

I tried this:

https://commission-detail.api.cj.com/v3/commissions?date-type=posting&start-date=2011-02-15&end-date=2011-02-17

And I get the message "You must specify a developer key"

I have a developer key, but I dont know where to place it

Thanks!
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37758150
If you look at the documentation for the request:

http://help.cj.com/en/web_services/web_services.htm#Commission_Detail_Service.htm

the value is passed in the request headers:


Commissions Resource Request Parameters

Header = authorization
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:pvg1975
ID: 37758172
Yes, I noticed that, but I dont know how to pass the header, and I get an error when trying to add https://commission-detail.api.cj.com/v3/commissions as a service reference in VB.NET
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37758191
REST services are not web services that you would be able to add a service reference to.  Adding a header value depends on what you are using to send the web request.

Example:

WebHeaderCollection.Add Method (String, String)
http://msdn.microsoft.com/en-us/library/y280s7kx.aspx

 'Create a web request for "www.msn.com".
            Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.msn.com"), HttpWebRequest)

            'Get the headers associated with the request.
            Dim myWebHeaderCollection As WebHeaderCollection = myHttpWebRequest.Headers

   	    Console.WriteLine("Configuring Webrequest to accept Danish and English language using 'Add' method")

	    'Add the Accept-Language header (for Danish) in the request.
            myWebHeaderCollection.Add("Accept-Language:da")

            'Include English in the Accept-Langauge header. 
            myWebHeaderCollection.Add("Accept-Language","en;q" + ChrW(61) + "0.8")

Open in new window

0
 

Author Comment

by:pvg1975
ID: 37758256
Thanks!

I tried this code:

        Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("https://commission-detail.api.cj.com/v3/commissions"), HttpWebRequest)
        Dim myWebHeaderCollection As WebHeaderCollection = myHttpWebRequest.Headers
        myWebHeaderCollection.Add("authorization:**** Private information removed ***")
        Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
        MsgBox(myWebHeaderCollection)
        myHttpWebResponse.Close()

But i get this error:

The remote server returned an error: (400) Bad Request.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37758600
Here is a problem with a REST service--if there is an error on the server it always returns 400 - Bad Request.

I would try this instead:

myWebHeaderCollection.Add("authorization", <authorization key>)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37758604
*** Private information removed ***

.NET Zone Advisor
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37758625
Also, you need to conform to the URL for each type of request.  All required elements must be specified.  If you look at the help for the web service, it explains what is optional and what is required.

Request examples:

https://commission-detail.api.cj.com/v3/commissions?date-type=event

https://commission-detail.api.cj.com/v3/commissions?date-type=posting
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37758630
Another thing to think about is getting the HttpWebResponse, getting the response stream, and extracting the text from the stream using a StreamReader.  The server might return information that can help you debug a request.
0
 

Author Comment

by:pvg1975
ID: 37758757
Thanks TLO,

I tried this and still get the same error:

        Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("https://commission-detail.api.cj.com/v3/commissions?date-type=posting&start-date=2012-02-15&end-date=2012-03-21"), HttpWebRequest)
        Dim myWebHeaderCollection As WebHeaderCollection = myHttpWebRequest.Headers
        myWebHeaderCollection.Add("authorization", "<My_Key>")
        Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
        MsgBox(myWebHeaderCollection)
        myHttpWebResponse.Close()

Im kinda feeling like an idiot lol... I just cant make it work :(

VB is returning the error on this line:

Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)

Thanks for your help again.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 37759038
Here is a perfect opportunity to learn something, because this is a common problem with REST web services.  

1) This is an HTTPS address, which can be little tricky with HttpWebRequest.

2) You need to match up the expected request method (GET, PUT, POST, DELETE).  

3) You can use Fiddler (an HTTP Debugger) to generate a known web request.

Fiddling with Fiddler – debugging REST services on your machine
http://herbjorn.wordpress.com/2010/04/27/fiddling-with-fiddler-debugging-rest-services-on-your-machine/

4) Are you examining the web response text?
0
 

Author Comment

by:pvg1975
ID: 37759705
Hi TLO!

Looks like I made it work, I get an XML as a response!

I will create a new question regarding this in a few minutes.

Thanks!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question