Solved

Grabbing content from an external web server

Posted on 2004-10-03
3
348 Views
Last Modified: 2008-03-03
The application in question is ASP.NET / C#. I have a webform with some text boxes, and it submits via the POST method to a web server out of my control. That web server displays a report in html to the user in the browser window.

What I would like to do is:

- Keep the user on my site instead of leaving to the other site.
- Save the contents of the report to a database.
- Show the report in a new browser window from the content stored in the database in the previous step.

Can somebody point me in the right direction on how to do this?
0
Comment
Question by:MetaDataY
3 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 12213532
Try using this function to grab the content:

Function GetHTML(strURL)
      Dim objXMLHTTP, strReturn
      Set objXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")
      objXMLHTTP.Open "GET", strURL, False
      objXMLHTTP.Send
      strReturn = objXMLHTTP.responseText
      Set objXMLHTTP = Nothing
      GetHTML = strReturn
End Function
0
 
LVL 5

Accepted Solution

by:
ajitanand earned 350 total points
ID: 12214693
fritz - your solution relates to ASP and that too for just HTTP GET. FOR ASP.Net+C# + HTTP POST, here is my approach:


1. Get the text box values and submit these to the remote server using the dot net webclient class
-------------------------------------
public void PostFormData(){
      string uriString = "http://myremoteserver.com/report.asp";

      // Create a new WebClient instance.

      WebClient myWebClient = new WebClient();

      //data to be posted to the URI
      //You will get this form TextBox1.Text and so on...
      string myTextBox1Value = "somevalue";
      string myTextBox2Value = "somevalue";
      string myTextBox3Value = "somevalue";
      
      //Build the post data in url encoded format
      string postData = "TextBox1=" + HttpUtility.UrlEncode(myTextBox1Value) +
            "&TextBox2=" + HttpUtility.UrlEncode(myTextBox2Value) +
            "&TextBox3=" + HttpUtility.UrlEncode(myTextBox3Value);

      myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");

      // Apply ASCII Encoding to obtain the string as a byte array.
      byte[] byteArray = Encoding.ASCII.GetBytes(postData);

      // Upload the input string using the HTTP 1.0 POST method.
      byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray);
      myWebClient.Dispose();

      // Get and Decode the response.
      string myResponseText = Encoding.ASCII.GetString(responseArray);
      
      //display the response, if you want
      //Response.Write(myResponseText);
      
      //store it in your db as well
      //some db updates... here...
      //and lets assume you get the new report record ID as myresponsetextID after storing in DB
      
      //write the following javascript that opens a new popup window (another aspx page),
      //that will throw the response content we stored here in the db
      //Response.Write("<script" = ">window.open('reportpage.aspx?recordid=" + myresponsetextID +"','report');</" + "script>");

      //Done!
}


2. Now you have stored the page output in your db, and a pop up appears for the user that points to another page (reportpage.aspx with a querystring parameter - recordid). This page will read the database content (the response stored earlier) and render it to the client.

-------------------


Hope this helps.

rgds,
Ajit Anand
0
 

Author Comment

by:MetaDataY
ID: 12214877
Ajit Anand, that's what I'm looking for. I had actually found how to do this with HttpWebRequest and HttpWebResponse, where you have to specify content length and stream to the request and streamread the response. WebClient seems to be even easier.

Thank you both for your input!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now