Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Virtual Browsing

Status
Not open for further replies.

0ddball

Technical User
Nov 6, 2003
208
GB
I can assure you that I don't intend to cheat on any MMORPG games with the following question :)

I need to fill in a form on another server which only allows posts from that server. I only found this out after I'd written a FakeForm class. I wasn't happy about this :D

So. I either need a way to alter the header of my HttpRequest to make it believe that I'm submitting from that server or I need a way to actually physically fill in the form (without having to hack IE - I mean get a copy of the page live in memory, fill it in and hit the page's Submit button).

This is a really tricky one for me. The people I'm working with (and who own the other site) are completly useless. They could easily expose this service as a WebService but don't - their code is horrendous and doesn't pass company code validity policy. So I have to interop.

Come on folks, I know some of you must cheat at Planetarion :)

C# please :)


Yet another unchecked rambling brought to you by:
Oddball
 
I'm not sure if you're going to be able to do this. If the server is checking to see where the post is coming from, you probably won't be able to fool it into thinking it os coming from it's own server.

You could try making a httpWebRequest and pass the details in that way but I'm not sure if it will work as we don't know exactly what the server is doing in terms of validation. It's worth a try though...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi Ca8,

Yeah - it's a swine this one.

I'll post the code I've used to remotely submit the form for everyone to have a look at if they are interested.

This bit manages the form:
Code:
public class FakeForm
{
	private string formName;
	private string actionUrl;
	private Dictionary<string, FakeFormControl> hControls = new Dictionary<string, FakeFormControl>();

	public FakeForm(string formName, string actionUrl)
	{
		this.formName = formName;
		this.actionUrl = actionUrl;
	}

	public void ResetForm()
	{

	}

	public HttpWebRequest PrepareSubmitFormRequest()
	{
		// Set up encoding/decoding services
		ASCIIEncoding encoding = new ASCIIEncoding();
		HttpServerUtility u = HttpContext.Current.Server;
		

		string preparedString = "";
		bool isFirst = true;

		foreach (FakeFormControl ffc in hControls.Values)
		{
			if (!isFirst)
				preparedString += "&";
			isFirst = false;
			preparedString += ffc.fieldName + "=" + u.UrlEncode(ffc.fieldValue);
		}

		byte[] ba = encoding.GetBytes(preparedString);

		// Prepare web request...
		HttpWebRequest hReq = (HttpWebRequest)HttpWebRequest.Create(actionUrl);
		hReq.Method = "POST";
		hReq.ContentType = "application/x-[URL unfurl="true"]www-form-urlencoded";[/URL]
		hReq.ContentLength = ba.Length;

		Stream bodyStream = null;
		bodyStream = hReq.GetRequestStream();
		try
		{
			// Send the data.
			bodyStream.Write(ba, 0, ba.Length);
		}
		catch (IOException ioe)
		{
			System.Diagnostics.Debug.Write(ioe);
		}
		finally
		{
			bodyStream.Close();
		}

		return hReq;
	}

	public FakeFormControl this[string i]
	{
		get
		{
			return hControls[i];
		}

		set
		{
			hControls.Add(i, value);
		}
	}

	public Dictionary<string, FakeFormControl> Controls
	{
		get
		{
			return hControls;
		}
	}
}

public struct FakeFormControl
{
	public string fieldName;
	public string fieldValue;
	public string fieldType;

	public FakeFormControl(string fieldName, string fieldValue, string fieldType)
	{
		this.fieldName = fieldName;
		this.fieldValue = fieldValue;
		this.fieldType = fieldType;
	}
}

And this bit submits it:

Code:
lffc.Add(new FakeFormControl("submit1", "Register/Update", "submit"));

		foreach (FakeFormControl ffc in lffc)  // A list of form controls
		{
			ff[ffc.fieldName] = ffc;
		}

		HttpWebRequest hReq = ff.PrepareSubmitFormRequest();

		try
		{
			hResp = hReq.GetResponse();
			HttpWebResponse hwRes = (HttpWebResponse)hResp;

			Stream ios = hwRes.GetResponseStream();

			List<byte> ba = new List<byte>();
			int read = 0;
			long count = 0;

			while ((read = ios.ReadByte()) >= 0)
			{
				ba.Add((byte)read);
				count++;
			}

			replyString = System.Text.Encoding.ASCII.GetString(ba.ToArray());
		}
		catch (Exception ex)
		{
			System.Diagnostics.Debug.Write(ex);
		}

		if (replyString.Contains("Record Created Successfully"))
		{
			string drStr = replyString.Substring(replyString.IndexOf("<b>") + 3, 6);

			DezRezUser dru = new DezRezUser(drStr, AGENCY_ID.ToString(), BRANCH_ID.ToString());

			return dru;
		}
		else
			return null;

Don't know if that helps me or anyone else but it's good to share.


Yet another unchecked rambling brought to you by:
Oddball
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top