Click here to Skip to main content
14,208,441 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
hi.

there is a website "http://sim.mci.ir/first-step" that sale mobile sim cards.
there is a function in this site that sends request to "http://sim.mci.ir/get-free-nums[^]" and gets available numbers.

i want to do this function in vba (excel) and save numbers in cells.
but it doesn't work!

this ajax function in html works, even if i use 'Call CurrentWindow.execScript...' in vba:

var range='9129370';
$.ajax({url: 'http://sim.mci.ir/get-free-nums',data: {range: range},type: 'GET'}).done(function (response) {
response = JSON.parse(response);
var unsortedNumbers = response.numbers;
if (response.status != 'OK') {
$('.suggestion-failed').html('No Service').show(); 
return;};
if (unsortedNumbers.length == 0) {
$('.suggestion-failed').html('No Range').show(); return;};
var bestMatches = findBestMatchForSuggestion(
unsortedNumbers , '9129370');
if (bestMatches.length != 0) {
$('.suggestion-failed').html('No Range 2').show(); return;};
$('.suggestion-failed').html('Success').show();return;});



but when i use vba code, there is no responseText!!
Set MyRequest = CreateObject("MSXML2.XMLHTTP.6.0")
MyRequest.Open "GET", "http://sim.mci.ir/get-free-nums", False
MyRequest.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
MyRequest.Send "{range: 9129370}"


what shoud i do? is it pussible to do this in vba directly?
Posted
Updated 9-Jul-14 9:09am
v2
Comments
Maciej Los 9-Jul-14 15:11pm
   
Try with: Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Please, try this:
Option Explicit

Sub DoSomeJob()
Const sUrl As String = "http://sim.mci.ir/get-free-nums"

Dim oRequest As WinHttp.WinHttpRequest
Dim sResult As String

On Error GoTo Err_DoSomeJob

Set oRequest = New WinHttp.WinHttpRequest
With oRequest
    .Open "GET", sUrl, True
    .SetRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
    .Send "{range:9129370}"
    .WaitForResponse
    sResult = .ResponseText
    Debug.Print sResult
    sResult = oRequest.Status
    Debug.Print sResult
End With



Exit_DoSomeJob:
    On Error Resume Next
    Set oRequest = Nothing
    Exit Sub
    
Err_DoSomeJob:
    MsgBox Err.Description, vbExclamation, Err.Number
    Resume Exit_DoSomeJob

End Sub


Returned values:
{"status":"OK","numbers":[]}
200



More about:
WinHttpRequest object[^]
Retrieving Data Using Script[^]
VBA Web Requests[^]
WinHttp.WinHttpRequest.5.1[^]


Good luck!
   
v2
Comments
Member 10935376 23-Jul-14 19:19pm
   
Thank you!
but the parameter should not be in .send, I found that I should use this command:
.Open "GET", sUrl & "?range=9129370", True
Maciej Los 24-Jul-14 2:13am
   
You're welcome ;)
If my answer was helpful, please mark it as a solution (green button) - formally to remove the question from unanswered list.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190612.1 | Last Updated 9 Jul 2014
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100