Jump to content

IIS proxy without php


rhodges

Recommended Posts

Posted

If you don't want to enable php and you are hosting HAD in IIS, you can use the following ashx (.NET) proxy instead of the isyProxy.php.

 

Name it isyProxy.ashx, and adjust your url rewrite rule inside your web.config to use it.

Note: I may not have set it up correctly, but I had to adjust my url rewrite. Any experts, feel free to chime in:

 

web.config


<?xml version="1.0"?>
 
<!--
  For more information on how to configure your ASP.NET application, please visit
  -->
 
<configuration>
 
  <appSettings>
    <add key="userName" value="xxxx"/>
    <add key="password" value="xxxx"/>
    <add key="ipAddress" value="xxx.xxx.xxx.xxx"/>
  </appSettings>
 
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
 
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Imported Rule 1" stopProcessing="true">
          <match url="^rest/" ignoreCase="false" />
          <action type="Rewrite" url="isyProxy.ashx" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
 
</configuration>

IsyProxy.ashx

Note: I included the replacing spaces with %20 I saw others use in their php version. Not sure if needed.  I haven't tested.

<%@ WebHandler Language="C#" Class="isyProxy" %>

using System;
using System.Xml.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.Configuration;

public class isyProxy : IHttpHandler {

    private static string UserName
    {
        get { return ConfigurationManager.AppSettings["userName"]; }
    }
    
    private static string Password
    {
        get { return ConfigurationManager.AppSettings["password"]; }        
    }
    
    private static string IPAddress
    {
        get { return ConfigurationManager.AppSettings["ipAddress"]; }
    }
    
    private static string Get(string url, string userName, string password)
    {
        using (var client = new WebClient())
        {
            client.Credentials = new NetworkCredential(userName, password);

            client.Headers.Add("Cache-Control", "no-cache, must-revalidate");
            client.Headers.Add("Expires", "Sat, 26 Jul 1997 05:00:00 GMT");
            client.Headers.Add("Content-Type", "application/json");

            return client.DownloadString(url);
        }
    }
    
    public void ProcessRequest (HttpContext context) {

        string req = context.Request.ServerVariables["REQUEST_URI"];
        
        if (req.StartsWith("/"))
            req = req.Substring(1);

        req = req.Replace(" ", "%20");

        string url = String.Format("http://{0}/{1}", IPAddress, req);

        try
        {
            string xml = Get(url, UserName, Password);

            context.Response.ContentType = "text/xml";
            context.Response.Write(xml);
        }
        catch (WebException wex)
        {
            context.Response.TrySkipIisCustomErrors = true;
            context.Response.ContentType = "text/xml";
            
            var res = ((HttpWebResponse)wex.Response);
            
            context.Response.StatusCode = (int)res.StatusCode;
            context.Response.StatusDescription = res.StatusDescription;

            var x = new XElement("RestResponse"
                        ,new XAttribute("succeeded", "false")
                        ,new XElement("status", (int)res.StatusCode)
                        //,new XElement("statusMessage", wex.Message)
                    );

            context.Response.Write(x.ToString());
        }
    }
 
    public bool IsReusable {
        get {
            return true;
        }
    }

}

It will return errors that same way I saw Isy return them. Isy doesn't return the message, just the status code. If you want the message, uncomment out the statusMessage line.

 

I have spent about an hour total on HAD and the proxy.  It may need to be tweaked.  Cheers!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...