1

I am trying to setup a simple proxy server that we post the data to my proxy server. The proxy server will take the posted data forward it on to the actual server and get the response from the actual server. Then display the response on the proxy server which the website that made the request reads and does whatever with the data. I am having trouble with the first part in getting the raw post data that is coming from the website. It appears the asmx file always wants to do things off of parameters but my proxy just want to forward on the raw request. It does not know the parameters. Below is an example request to the proxy server: localhost/mobile.asmx POST {"userName":"fake@email.com","password":"xxxx","appID":"2302FF64-925D-4E0E-B086-73AA9FF152D8"}

Once again I do not want to just get the username and password. I want to capture the full raw request and forward it on to the real server.

I have tried tons of things. Because there is no request parameter I can not use request. I also believe the function GETUSERTOKENLOGIN takes place after the stream of raw post data is read so I can no longer use a stream to get the data. I have tried quite a few things.

I want this to be a super simple script if possible. Below is my super simple example. I know I can just add a wrapper around the data but I would like to not have to do that.

Any help would be greatly appreciated. MOBILE.ASMX

<%@ WebService Language="C#" Class="mobile" %>

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Net;
using System.IO;
using System.Web.Script.Services;
using System.Text;
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class mobile : System.Web.Services.WebService 
{
    public mobile()
    {


    }

    // The HelloWorld() example service returns the string Hello World.
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetUserTokenLogin()
    {
        // Create a new request to the mentioned URL.
        WebRequest myWebRequest = WebRequest.Create("http://api.geonames.org/citiesJSON");
        myWebRequest.Method = "POST";
        Stream dataStream = myWebRequest.GetRequestStream();
        WebResponse myWebResponse = myWebRequest.GetResponse();

        // Print the  HTML contents of the page to the console.
        Stream streamResponse = myWebResponse.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);
        Char[] readBuff = new Char[256];
        int count = streamRead.Read(readBuff, 0, 256);
        String FullData = "";
        while (count > 0)
        {
            String outputData = new String(readBuff, 0, count);
            FullData = FullData + outputData;
            count = streamRead.Read(readBuff, 0, 256);
        }

        // Close the Stream object.
        streamResponse.Close();
        streamRead.Close();

        myWebResponse.Close();
        return FullData;
    }
}
David Corrado
  • 363
  • 3
  • 19

3 Answers3

2

To get the raw JSON of a request in your ASMX (for example, you have made a POST request to it from AngularJS), try the following code:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Calculate()
{
    HttpContext.Current.Request.InputStream.Position = 0;
    var jsonString = new StreamReader(HttpContext.Current.Request.InputStream, Encoding.UTF8).ReadToEnd();
    var json = JObject.Parse(jsonString);

    // your code
}

Note that you shouldn't need a using statement here, as you are not the one disposing the InputStream.

  • @user3638471: Oh, man! I've spent a couple of days looking for this!! These 3 lines of code has saved me. – cChacon Jun 18 '19 at 19:57
0

Parsing the request is the job of your application server.

Your proxy server should not have any application code on it. If all you want to do is use IIS as a proxy server, refer: Setting up IIS as a reverse proxy.

If you only want the raw request and want to write out your raw response, you could create a custom HttpModule.

Refer: Creating a custom HttpModule

In this module, you can get the request as the client sent it, and forward it to another server, and then, take the response from that server and forward it to your client. (In effect you are making a reverse proxy.)

There is no way you could achieve this within a asp.net webservice.

nunespascal
  • 17,584
  • 2
  • 43
  • 46
  • Is there any way to get the raw post from the asmx file? I am just building the file. I would prefer my client to not have to go through all that configuration. Is it possible to get the raw post to the service so I can forward it on to the server. If I was just able to do that then the script I have would work fine. – David Corrado Jun 25 '12 at 15:12
  • I edited my reply, raw posts need you to implement `IHttpModule`. Take a look. – nunespascal Jun 26 '12 at 03:56
  • Thanks. For your time. I found this solution very helpful: http://www.codeproject.com/Articles/31329/Simple-Reverse-Proxy-in-C-2-0-description-and-depl All I had to do was download the demo which gives you a dll and a webconfig. I will put more exact information in my original question. Thanks – David Corrado Jun 26 '12 at 16:56
0

I ended up downloading the demo from below: http://www.codeproject.com/Articles/31329/Simple-Reverse-Proxy-in-C-2-0-description-and-depl It gives you a bin folder which I just put in my root directory. Than I created a folder called mobile.asmx. In that folder I put the webconfig from the demo in it with only a change of the remote server

<appSettings>
 <add key="RemoteWebSite" value="http://my-clients-server.com/mobile_dev/" />
</appSettings>

<system.web>
  <httpHandlers>
   <add verb="*" path="*" type="ReverseProxy.ReverseProxy, ReverseProxy"/>
 </httpHandlers>
</system.web>

So whenever The website requests the current domain for example: www.currentdomain.com/mobile.asmx/MYSERVICE It forwards on that request to the remote website like below: http://my-clients-server.com/mobile_dev/mobile.asmx/MYSERVICE

I tested the above with both XML and JSON both which I needed.

David Corrado
  • 363
  • 3
  • 19