views:

460

answers:

5

Hi

Please consider the following scenario,

There are two web applications App1 & App2. A user would submit his information on App1 though a form. On click of a specific button/link on App1, the same data should be posted to a page on App2 and the user should also be redirected to the same page on App2.

I would like some help in finding out the best way to implement this functionality.

One of the approaches that I have already tried out is by creating a temporary HTML form at runtime, setting the action attribute of the form to the App2 Page and get the form posted by using javascript submit. The data can then be fetched on App2 page by using the response.form object.

This approach works well, but i was still wondering if there is any other way to implement the required functionality.

I would really appriciate if you can give some insights on using RESTful webservices to implement this, or else, using some HttpModule to intercept requests at App1 and modify redirect response to app2 or any other approach that you might find fit for the purpose.

Edit: Using querystring isnt an option for me.

A: 

I have built something like what you are describing, and I found that using a <form> tag to POST to app2 is the most reliable way... basically, the way you found that worked well.

Josh
Im sorry i couldnt understand your approach.. using a TAG to POST ? can you please elaborate... ?
Danish
Haha, sorry, my HTML code was being parsed! I have backticked it.
Josh
A: 

If you are already committed to using javascript, then why not an ajax post, and change the window.location based on the response?

Bill Bingham
Ajax post can be used with a webservice listening on app2 .. but the service would have to hold the data in some way or ther other.. and i am not sure how would that happen.. Do you have some suggestions on how to store the data on the App2 using a webservice, which can be used when the browser opens the page after being redirected from app1?
Danish
A: 

You can use HttpServerUtility.Transfer this will preserve your form contents and transfer the user to the new page.

http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.transfer.aspx

iaimtomisbehave
I think, Server.Transfer would work within the same application and that too without actually posting the data.. do you have some code example to illustrate your suggestion..
Danish
A: 

If App2 is residing on a different domain, it's usually best to create your own interface for the submission, and have that interface handle the posting from App1 to App2.

(Browser) -> Submits form to App1 -> 
    (App1) -> validate input
           -> stores local info
           -> creates an HttpRequest/POST object
           -> posts to App2
      (App2) -> handles the post
      <- returns the response
    -> confirms the results of App2
  <- returns the results to the browser.

In essense, you want to control and proxy requests from your Applications domain to any outside interfaces as much as possible.

Tracker1
A: 

pffft ...

I've had a need to do similar things with feed agregation and building rss feeds from web page content on different domains.

User Gets app1 page, fills in details and submits then on the server for app1 I have a method that looks like this ...

HTMLDocument FetchURL( string url )
{
    WebClient wc = new WebClient();
    string remoteContent = wc.DownloadString(url);
    // mshtml api is very weird but lets just say you have to do things this way ...
    HtmlDocument doc = new HTMLDocument();
    IHTMLDocument2 doc2 = (IHTMLDocument2)doc;
    doc2.write(new object[] { remoteContent });

    return (HTMLDocument)doc2;
}

This function does 2 things of use ...

  1. It gets the page of content at "url"
  2. It parses that content in to a HTMLDocument object

Once you have this function you can then call it passing it the url to the remote page and get back a html doucment. The functions in the HTMLDocument object will allow you to do javascript like dom queries such as :

docObject.GetElementById("id");

I then have different functions that do different things with this object based on the page / site i'm returning data from.

There is however one fatal flaw here ...

This is likely to work really well with sites that don't change much in structure and are built by code but not so well on less dynamic sites.

With stackoverflow for example its easy to pull out a question and the accepted answer for that question so I could use this code to pull and publish content from here on my own web site.

However ...

This is not going to help you for user / login related details as this sort of information is not shared to generally everyone.

It's bit like me going and trying this to link facebook profiles to my own website, I would have to go through some form of api that asked the user to authenticate their details before making the request.

simply pulling a web page based on a url only will give the other site no authentication information unless that site accepts the user login details in the quesrystring and you already have them.

You may however be able to chain requests by ripping apart my sample method, requesting the login page parsing the results, filling in the form, then posting back using the same web client instance to login then requesting the url.

The idea being that you would have a form that asks the user to put in their login details for the remote site on your site then you go and find their profile page based on that.

This would be best farmed out to a class rather than just a simple method like i have here.

In my case though i was only after something simple (the bbc top 40 uk charts) which i pulled information from not only the bbc but places like amazon, google, and youtube, then i built a page :)

It's neat but serves no functional purpose other than pulling all your other fave sources of info on to 1 page.

Wardy