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;
}
}
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.