Using asp.net and VB (or C# which I can translate) to connect to FEDEX via SSL (question)

Discussion in 'ASP.NET 2.0' started by superwizbang, Mar 20, 2007.

  1. Any folks out there placed code which talks to the FEDEX servers via a SSL connection? As I have read (and understand), there are two means by which you can interface with their systems...a webservice API and direct HTTPS requests. I am looking for code examples which others have used to do such. I have build a shopping cart and have two items left, to get shipping rates and CC processing. Step one is the shipping. Your imput is very well appreciated.

    Thanks!

    -Ken
     
  2. haven't used, but i'm assuming you'll have to sign up for the fedex api. when you sign up they should give you some code samples.

    have you signed up to use their api?


    Joel Thoms
    DiscountASP.NET
    http://www.DiscountASP.NET
     
  3. Sorry, I have been out of town. Yes, I signed up but I am more of a visual learner and would like to pull apart some code. I looked at their examples, but none use a .Net 2.0 interface. There is a ASP.NET example on their site but I didn't garnish much information from it. Now that my SSL is up I will give it another look I think. I just didn't find any search results for FEDEX and wondered if anyone had some personal experience or notes to share.

    -Ken
     
  4. If you could provide some contact names, I would appreciate it. I keep running into walls on Google for 4 days now. If I were using PHP, I would be good. Since I am programming in VB, it seems the options are limited. The information on the FedEx site is disconnected at best. Being a small-time, hobbiest programmer, my learning is only what I teach myself. Some advanced concepts escape me. Without great examples to pick apart, I still find myself lost. The last resort will be to call their help line again, but they didn't seem too versed on their own software themselves last time. 8/

    -Ken
     
  5. Bruce

    Bruce DiscountASP.NET Staff

    I have seen a number of our users using it but that was a while back.


    I have not used it myself but it should be quite simple.


    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
  6. Thanks Wise.

    I was looking for something that was not "canned" but rather built from "page up". The link you posted to the FedEx site is the code I had poured over. The problem is it is written to interface with their API module...again, another canned version that has to be installed on the server you wish to use it on. Since this is FTP'd to DA servers where the API is not installed, their code will not work for me. 8/

    As I understand it, using a SSL connection to query their server requires only a "POST" method, providing a string of parameters. Their webservice crunches the params and returns the applicable information. They provide you a sandbox and you are to call them when your software is working correctly and they review you transactions to qualify your site. Having said that, I am trying to figure out if there is code out there which I can review that could show some build methods. Obviously there are many companies out there who have done this and gone on to market it for profit. I am just astonished that there are few published "freelance" versions out there and that FedEx has not made their examples better documented.

    To give you a better idea of what I am hoping to do, FedEx offers the following VB GUI example http://www.fedex.com/us/solutions/wis/pdf/fsm_directmanual.pdfon page 26. I can rip this apart and apply it to an ASP.NET pagewith a littletrial-and-error programming.

    A better example of what I am looking for is http://www.thescripts.com/forum/thread255602.html. My only problem is getting it going. I can't find *any* other examples to cross-reference. Do most people simply buy their shopping cart/shipping code from pre-canned authors? Maybe I should start to consider it?!? The closest I can get on Google is to query the criteria "gateway.fedex.com:443/GatewayDC" and read the results (which is the sandbox URL they want you to use during the dev process).

    -Ken
     
  7. Bruce

    Bruce DiscountASP.NET Staff

  8. For the sake of archive (and future questions), I will note what I found out today, this, the seventh day of looking into this FedEx Direct shipping solution (FedEx Direct=the non-API solution which you use a SSL connection to communicate queries with their webservice):
    1.) Review the 7-step implementation guide found at: http://www.fedex.com/us/solutions/wis/pdf/FedExAPIImplementationOverview.pdf?link=4
    2.) Review the Solutions page: http://fedex.com/us/solutions/wis/index.html
    3.) Register a new account: https://www.fedex.com/cgi-bin/shipapiDownload.cgi?link=4&first=y
    4.) Call the FedEx intergrations desk at 1.877.339.2774 , Monday-Friday 7am-12am, Saturday 7-7pm (CST) and at the voice prompt, state "Integration" to get a person knowing about the Direct solution. At this juncture, you will ask that they set up a test account in their sandbox ("gatewaybeta.fedex.com:443/GatewayDC").
    5.) Your first request to the test server (and eventually live server when your app has been certified) will be a subscription request for meter number. This is explained at: http://images.fedex.com/us/solutions/wis/pdf/API_Direct_Tagged_Transaction_Guide.pdf?link=4 on pages 11-16
    6.) After you have received your test meter number, you are ready to begin working with the test webservice to send and receive information. The VB code that I have garnished from other sites is as follows:




    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Dim SvcAddr As String = "gateway.fedex.com:443/GatewayDC"
    Dim SvcAddr As String = "gatewaybeta.fedex.com"
    Dim SvcPort As Integer = 443
    Dim SvcName As String = "/GatewayDC"
    Dim uriBuilder As UriBuilder = New UriBuilder("https", SvcAddr, SvcPort, SvcName)
    Dim uri As Uri = uriBuilder.Uri
    Dim request As HttpWebRequest = CType(WebRequest.Create(uri), HttpWebRequest)
    ' Set the method.
    request.Method = "POST"
    ' Set the user agent.
    request.UserAgent = "MyApplication"
    ' Set the content type.
    request.ContentType = "image/gif"
    ' Set the accept header.
    request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*"
    Dim qq As String = New String(New Char() {Convert.ToChar(34)})
    Dim body As String = ("0," _
    + (qq + ("402" _
    + (qq + ("29," _
    + (qq + ("821343443862" _
    + (qq + ("99," _
    + (qq + qq))))))))))
    Dim bytes() As Byte = Encoding.ASCII.GetBytes(body)
    ' Set the content length.
    request.ContentLength = bytes.Length
    Dim stream As Stream = request.GetRequestStream
    ' Write the bytes to the stream.
    stream.Write(bytes, 0, bytes.Length)
    Dim response As HttpWebResponse = CType(request.GetResponse, HttpWebResponse)
    End Sub

    This is very similar to the VB GUI example http://www.fedex.com/us/solutions/wis/pdf/fsm_directmanual.pdf on page 26. I aslo found another good free resource called DotNetShipping http://www.codeplex.com/dotnetshipping and was originally posted on the Microsoft site DotNet Workspaces. It is written in C# and a little complex but worth the review. Until the end of June, there is a messageboard with information about the DotNetShipping solution which can be found at: http://www.gotdotnet.com/workspaces/messageboard/home.aspx?id=29bbb7fe-2d18-4f82-a273-9445b978d1bf

    FedEx Shipping solutions documentation (codes, alerts, etc.) can be found at: http://fedex.com/us/solutions/shipapi/docs.html

    I will post the code that I use to initially connect with the test server and my transaction code when I have them working correctly. Hope this helps someone along the line who wants to build their own shipping solution on their website and does not want to spend the money to buy an already-marketed solution.

    (written by someone who is only a intermediate .NET coder)

    -Ken


    Post Edited (superwizbang) : 4/10/2007 1:40:47 AM GMT
     
  9. After a week off to play with other parts of my site, I am back at it. Here is the code that I used to make the subscription request to the FedEx server and receive my test meter number. You will need to modify the "sQueryString" variable to match your own personal information:


     
  10. On to getting rates from FedEx. I chose to use the DotNetShipping project located at http://www.codeplex.com/dotnetshipping to provide my shipping needs. I liked the way that the code was readily available for me to tweek if needed to suit my needs. One thing I might mention is that you will need to change the URL in the FedExProvider class in the Shipping Providers folder to point to https://gateway.fedex.com/GatewayDC during the development process...build it and use the compiled .DLL in your project. You will then switch back to the original https://gatewaybeta.fedex.com/GatewayDC location when your site has been approved by FedEx and you are ready to go live.

    I built the code below to parse the return string provided by FedEx via the DotNetShipping module. Obviously it returns a datatable with the columns RateDesc (a description of the rate to be chosen), Rate (the actual decimal value of price) and DeliveryDate (as calculated and provided by DotNetShipping) and sorted by price (decending--and as a side benefit, by time in-transit). This is helpful if you need to crunch these numbers later for, per se, a checkout page on a shopping cart. I chose to do the parsing here rather thanchange the module in case Idownloaded a new version to use at some point.

    I will post more code when I build the interface for registering and tracking shipments.

    -Ken
     
  11. Another week, a little more code. The function that I will post below requests your built tagged querystring which I have documented above in my meter request thread. Basically, visit the FedEx Tagged Reference PDF noted above and build your string accordingly; then pass that code into the FedExPost function below with your transaction type. There is little difference between tracking, requesting shipment, and deleting a shipment. Here is the code that I used to post each transaction.


     
  12. Two transactions which use the FedEx post code above are listed below. I will leave it up to anyone whois interested, to look up the querystring to ship a package. It is a little longer but built in the same method. There are XML methods to post to the FedEx servers as well, but I know little XML at this time. I didn't want to teach myself a language while fighting through all this as well. Lol. I will also place code below to allow you to parse particular tags for their desired text.

    Have fun!

    -Ken


     

Share This Page