Go Back   CodingForums.com > :: Server side development > ASP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-01-2011, 07:28 PM   PM User | #1
MissPhoenix
New Coder

 
Join Date: Oct 2010
Location: Norwich, CT
Posts: 35
Thanks: 4
Thanked 3 Times in 3 Posts
MissPhoenix is an unknown quantity at this point
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 07:35 PM.. Reason: (updating title)
MissPhoenix is offline   Reply With Quote
Old 04-01-2011, 09:28 PM   PM User | #2
MissPhoenix
New Coder

 
Join Date: Oct 2010
Location: Norwich, CT
Posts: 35
Thanks: 4
Thanked 3 Times in 3 Posts
MissPhoenix is an unknown quantity at this point
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
MissPhoenix is offline   Reply With Quote
Old 04-04-2011, 08:57 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
??? 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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
MissPhoenix (04-04-2011)
Old 04-04-2011, 09:30 PM   PM User | #4
MissPhoenix
New Coder

 
Join Date: Oct 2010
Location: Norwich, CT
Posts: 35
Thanks: 4
Thanked 3 Times in 3 Posts
MissPhoenix is an unknown quantity at this point
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 03:20 PM..
MissPhoenix is offline   Reply With Quote
Old 04-04-2011, 09:59 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
MissPhoenix (04-05-2011)
Old 04-04-2011, 10:00 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
MissPhoenix (04-05-2011)
Old 04-05-2011, 03:17 PM   PM User | #7
MissPhoenix
New Coder

 
Join Date: Oct 2010
Location: Norwich, CT
Posts: 35
Thanks: 4
Thanked 3 Times in 3 Posts
MissPhoenix is an unknown quantity at this point
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
MissPhoenix is offline   Reply With Quote
Old 04-05-2011, 03:31 PM   PM User | #8
MissPhoenix
New Coder

 
Join Date: Oct 2010
Location: Norwich, CT
Posts: 35
Thanks: 4
Thanked 3 Times in 3 Posts
MissPhoenix is an unknown quantity at this point
Resolved.

I would mark this thread as resolved, but it won't let me edit the original. =/
MissPhoenix is offline   Reply With Quote
Old 04-05-2011, 08:59 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
arin restful api, asp, json, whois

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:06 AM.


Advertisement
Log in to turn off these ads.