Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Oct 2010
    Location
    Norwich, CT
    Posts
    35
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Question ASP WhoIs Implementation (Arin) Help

    I'm trying to figure out how to get the Json response in ASP/JavaScript. I basically need the country code of an IP address to verify whether we have shipping rights there or not. I have all the other code, but I can't for the life of me figure out what in the world I'm supposed to do to implement this service into the browser.

    This is the API documentation:
    https://www.arin.net/resources/whoisrws/whois_api.html

    Basically from my understanding, I need to set the Header to Accept the Mime type application/json, from the arin link I query.

    This is the code I have, although I haven't tested it, I am so in the dark, I don't think I'd be getting what I need, which is the JSON object.

    I have googled galore for help, with no avail, so any help would be awesome; I'm completely clueless as to how to even go about this. The code below is pretty much my "shot in the dark" at it.

    Code:
    function getUserCountry(){
    	var strResult;
    	strURL = "http://whois.arin.net/rest/ip/"+Request.ServerVariables("remote_addr");
        
        try
        {
            // Create the WinHTTPRequest ActiveX Object.
            var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
            
            //  Create an HTTP request.
            var temp = WinHttpReq.Open("GET", strURL, false);
    
            //  Send the HTTP request.
            WinHttpReq.Send();
            
            //  Retrieve the response text.
            strResult = WinHttpReq.ResponseText;
        }
        catch (objError)
        {
            strResult = objError + "\n"
            strResult += "WinHTTP returned error: " + 
                (objError.number & 0xFFFF).toString() + "\n\n";
            strResult += objError.description;
        }
        
        //  Return the response text.
        return strResult;
    }
    Last edited by MissPhoenix; 04-01-2011 at 08:35 PM. Reason: (updating title)

  • #2
    New Coder
    Join Date
    Oct 2010
    Location
    Norwich, CT
    Posts
    35
    Thanks
    4
    Thanked 3 Times in 3 Posts
    My understanding is better now.

    I need to know how to configure a JSONRequest (including headers) for the
    Code:
    strURL = "http://whois.arin.net/rest/ip/"+Request.ServerVariables("remote_addr");
    The link will work, and what will come back is a JSON Object with all the info in it I need. :3

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,638 Times in 4,600 Posts
    ??? If you are using new ActiveXObject then this code must be for use in the browser, not in your ASP page. And if that is so, then it's also so that using new ActiveXObject means your code will only work in MSIE browsers. Do you care?

    But in any case, if this is an in-the-browser question, it's not an ASP question, per se.

    If you really intended this to be an ASP question, using JScript in ASP, then you should not use new ActiveXObject, you should use Server.CreateObject( ). But then I'd question the use of JSON. It would probably be easier to request an XML response and parse that. Which, yes, I do in ASP code (albeit with VBScript, not JScript).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    MissPhoenix (04-04-2011)

  • #4
    New Coder
    Join Date
    Oct 2010
    Location
    Norwich, CT
    Posts
    35
    Thanks
    4
    Thanked 3 Times in 3 Posts
    I actually figured it out... I'm sorry I never marked this as resolved got sucked into code-world.

    Here's my working function in the code, for anyone else looking for a solution:

    Code:
    function UserCountry(uIPAddr)
    {
        	var ipCountry = "null";
    	strURL = "http://whois.arin.net/rest/ip/"+uIPAddr;
        
        	var xmlhttp = new ActiveXObject("MSXML2.ServerXMLHTTP");
    	xmlhttp.open("GET", strURL, 0); 
    	xmlhttp.setRequestHeader("Accept", "application/json");
        	xmlhttp.send("");
      	var ftpRes = xmlhttp.responseText.toString(); 
    	var status = xmlhttp.status;
    
    	if(status == 200)
    	{
    		ipCountry = ftpRes;
    	}
    	else
    	{
    		strResult = 0 + "|noaccess";
    	}
    	
        	var xmlhttp = null;
    
        	return ipCountry;
    }
    Only its sending me the XML rather than the JSON. *sigh* when its not one thing, its another. I'm sure I'll figure it out though.

    Thank you for your help.

    (fixed the code to reflect the accept header)
    Last edited by MissPhoenix; 04-05-2011 at 04:20 PM.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,638 Times in 4,600 Posts
    You are using the wrong RequestHeader!!!

    Read the docs again: You need to specify the Accept header!

    Code:
        xmlhttp.setRequestHeader("Accept", "application/json");
    Setting content type was telling the service that your SEND message was in JSON format!! Luckily, it didn't believe you. <grin/>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    MissPhoenix (04-05-2011)

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,638 Times in 4,600 Posts
    But I still have to ask if you are doing this in ASP code or in browser code. Because if you are doing it in browser code, you are using an obsolescent object. MSIE 8 and above use the standard object that all the other browsers use and don't need to use ActiveXObject().
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    MissPhoenix (04-05-2011)

  • #7
    New Coder
    Join Date
    Oct 2010
    Location
    Norwich, CT
    Posts
    35
    Thanks
    4
    Thanked 3 Times in 3 Posts
    ASP Classic with JavaScript. And I fixed the code I presented, in case someone else needs to know in the future. Thank you, very much Pedant. You're always the one who answers my convoluted ASP questions... LOL

  • #8
    New Coder
    Join Date
    Oct 2010
    Location
    Norwich, CT
    Posts
    35
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Resolved.

    I would mark this thread as resolved, but it won't let me edit the original. =/

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,638 Times in 4,600 Posts
    Quote Originally Posted by MissPhoenix View Post
    ASP Classic with JavaScript.
    A final improvement, then.

    At least I thinks so.

    I heard many many years ago (about 10 years, in fact) that for server side code, you got better efficiency using Server.CreateObject than using ActiveXObject. It's quite possible that the advice is out of date, what with much newer version of the ASP engine in current IIS servers. But I would have code it thus:
    Code:
    var xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP");
    The reason, as I recall it, was that objects created in the context of the built-in SERVER container could be destroyed en masse and didn't need individual garbage collection.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •