Solved

Grabbing content from an external web server

Posted on 2004-10-03
3
357 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Automatically log-in a new user through code 1 59
Summernote required 3 137
ASP Focus problem 3 61
How to show result of a NULL value at random? 32 50
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 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