Reputation: 4824
I've got a HTTPHandler which returns a lump of HTML. What's the best way to inject this into a control on the server?
I've got it mostly working by using an asp:literal and using WebClient.downloadString() to grab the text from the handler
<asp:Literal runat="server" ID="Text_Page1" Visible="false"></asp:Literal>
<asp:Literal runat="server" ID="Text_Page2" Visible="false"></asp:Literal>
and then in the server-side methods:
Text_Page1.Text = new WebClient().DownloadString("http://localhost:666/" +sPage1URL);
Text_Page2.Text = new WebClient().DownloadString("http://localhost:666/" +sPage2URL);
The hardcoded web-address is just there for testing at the moment. Previously I tried just using "~/" +URL to try and build it up but the WebClient library threw an exception saying that the URL was too long (which is not true I don't think)
Any ideas on the best way to do this from the server-side?
Edit : When I say "Best" I mean most efficient and adhering to "best practices". My method doesn't work so well when it's put onto an authenticated IIS. I'm having trouble authenticating. I thought that doing
WebClient oClient = new WebClient();
oClient.Credentials = CredentialCache.DefaultCredentials;
oClient.UseDefaultCredentials = true;
String sData = oClient.DownloadString(sURL);
would work but i get a 401 error. Does anybody have any alternatives?
Cheers
Gordon
Upvotes: 2
Views: 2016
Reputation: 3154
Without asking any questions about the reasoning behind fetching html via a webrequest from the same application serving the contents of the include, i would wrap the functionality in a WebUserControl. Something along the lines of:
using System;
using System.Net;
using System.Web.UI;
public partial class HtmlFetcher : UserControl
{
//configure this somewhere else in the real world, web.config maybe
private const string ServiceUrl = "http://localhost:666/";
public string HtmlPath { get; set; }
protected override void Render(HtmlTextWriter output)
{
string outputText = String.Empty;
try
{
outputText = new WebClient().DownloadString(string.Format("{0}{1}", ServiceUrl, HtmlPath));
} catch (Exception e)
{
//Handle that error;
}
output.Write(outputText);
}
}
This is how you would add it to your page:
<%@ Register src="HtmlFetcher.ascx" tagname="HtmlFetcher" tagprefix="uc1" %>
<uc1:HtmlFetcher ID="HtmlFetcher1" HtmlPath="some-test-file.html" runat="server" />
Upvotes: 1
Reputation: 153
You can use server side code blocks into your aspx with <% %>
.
Try this:
<% new WebClient().DownloadString("http://localhost:666/" +sPage1URL) %>
Upvotes: 0
Reputation: 5912
you will get the data in the string lcHtml then use it as you want
// *** Establish the request
string lcUrl = "http://yourURL";
HttpWebRequest loHttp =
(HttpWebRequest) WebRequest.Create(lcUrl);
// *** Set properties
loHttp.Timeout = 10000; // 10 secs
loHttp.UserAgent = "Code Sample Web Client";
// *** Retrieve request info headers
HttpWebResponse loWebResponse = (HttpWebResponse) loHttp.GetResponse();
Encoding enc = Encoding.GetEncoding(1252); // Windows default Code Page
StreamReader loResponseStream =
new StreamReader(loWebResponse.GetResponseStream(),enc);
string lcHtml = loResponseStream.ReadToEnd();
loWebResponse.Close();
loResponseStream.Close();
Upvotes: 1