Solved

Grabbing content from an external web server

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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 …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

15 Experts available now in Live!

Get 1:1 Help Now