Thursday, 11 December 2014

Microsoft Excel - Macro that sends httpWebRequest

watch this example on YouTube


Here is the macro 
Sub RunHttpRequest()
  Dim req As Object
  Set req = CreateObject("WinHttp.WinHttpRequest.5.1")
  req.Option(0) = "Test"
  req.Open "GET", "http://localhost:64391/HttpWebResponseTest.aspx"
  req.Send
  MsgBox req.ResponseText
 
End Sub


as on video - first let's create simple web page that returns web response
this is C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestingWeb
{
    public partial class HttpWebResponseTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    switch (Request.UserAgent.ToString().Trim())
                    {
                        case "Test":
                            {
                                Response.Write("Message from external page");
                                break;
                            }
                        default:
                            {
                                Response.Write("not valid request");
                                break;
                            }
                    }
                }
               
            }
            catch{}
        }
    }
}



No comments:

Post a Comment