How to convert a URL to HTML in Xamarin?

Dinesh_OfficialDinesh_Official Member ✭✭
edited January 16 in Xamarin.Forms

Hello,

I have a requirement that is need to convert a URL string to HTML string. From the below code snippet,

strings are converted but if url has image or gif, it is not converting. Can any one please suggest?

public static string GetHTMLFromURL(string url = "https://www.google.com/")
{
string htmlstring = string.Empty;.
using (WebClient client = new WebClient())
{
htmlstring = client.DownloadString(url);
}

return htmlstring;

}

I tried the below link but no use

https://stackoverflow.com/questions/16642196/get-html-code-from-website-in-c-sharp

Don't want to use third party library.

Answers

  • aga913aga913 Member ✭✭

    do you mean you want to get the websites HTML code ?
    can you provide a link that is not working ?

  • ColeXColeX Member, Xamarin Team Xamurai
    edited January 16

    Try to use the following code ,it works fine on my side , the image is converted as expected .

    HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url); 
    HttpWebResponse response = (HttpWebResponse) request.GetResponse(); 
    StreamReader sr = new StreamReader(response.GetResponseStream()); 
    richTextBox1.Text = sr.ReadToEnd(); 
    sr.Close(); 
    

  • JohnHardmanJohnHardman GBUniversity mod
    edited January 16

    @ColeX said:
    Try to use the following code ,it works fine on my side , the image is converted as expected .

    HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url); 
    HttpWebResponse response = (HttpWebResponse) request.GetResponse(); 
    StreamReader sr = new StreamReader(response.GetResponseStream()); 
    richTextBox1.Text = sr.ReadToEnd(); 
    sr.Close(); 
    

    The usual recommendation is to use HttpClient in place of HttpWebRequest

  • Dinesh_OfficialDinesh_Official Member ✭✭

    @aga913 , https://www.google.com/. Google image is not downloading

  • Dinesh_OfficialDinesh_Official Member ✭✭

    @ColeX , unfortunately images are not downloading

  • Dinesh_OfficialDinesh_Official Member ✭✭

    @JohnHardman ,

    I tried with the below code too

    var client = new HttpClient();
            htmlstring = await client.GetStringAsync(url);
    

    but it is not downloading

  • DirkWilhelmDirkWilhelm USMember ✭✭✭✭

    You are downloading the html text of the webpage, its sourcecode. Why do you expect that the used images in that code are also downloaded?

    What are you trying to achive here?

  • Dinesh_OfficialDinesh_Official Member ✭✭

    @DirkWilhelm , Need to convert an html file to PDF file. I have a url which has images and text and i need to get the source code as HTML string which will converted into PDF later.

  • ColeXColeX Member, Xamarin Team Xamurai

    So what you want isn't html string, but the pdf generated from webview , take a look at https://codemilltech.com/xamarin-forms-e-z-print/ , you need dependency service to implement on each specific platform .

  • Dinesh_OfficialDinesh_Official Member ✭✭

    @ColeX , no sorry if i am confusing you. i just to get a html string

  • ColeXColeX Member, Xamarin Team Xamurai

    @Dinesh_Official said:
    @ColeX , no sorry if i am confusing you. i just to get a html string

    The html string you get is not complete code , it may miss image , css style or something else , i don't think it can be converted into pdf directly .

  • Dinesh_OfficialDinesh_Official Member ✭✭

    For example, i copy the HTML string and save as text file and run in browser, the page should display with images

  • JohnHardmanJohnHardman GBUniversity mod
    edited 12:00PM

    @Dinesh_Official said:
    I tried with the below code too

    var client = new HttpClient();
            htmlstring = await client.GetStringAsync(url);
    

    but it is not downloading

    For the HttpClient equivalent of the HttpWebRequest code that @ColeX suggested above, try GetAsync or possibly GetByteArrayAsync

    However, you seem to fundamentally be misunderstanding the structure of pages in HTML, in particular how images are handled. Yes, you can use HttpClient to get the pieces that make up a page in a browser, however it is not a case of making one request and getting one response. Browsers make multiple requests to get all of the assets required to display a typical page. Each image will typically require its own request, separate from the initial request for the page's HTML.

  • JohnHardmanJohnHardman GBUniversity mod

    @Dinesh_Official said:
    For example, i copy the HTML string and save as text file and run in browser, the page should display with images

    It's not that simple. Even if you wrote the appropriate code to download all of the assets used by a web page, there is no guarantee that then opening the page from your local files would have the desired result. It all depends on how the page was written in the first place.

  • JohnHardmanJohnHardman GBUniversity mod

    @ColeX said:
    So what you want isn't html string, but the pdf generated from webview , take a look at https://codemilltech.com/xamarin-forms-e-z-print/ , you need dependency service to implement on each specific platform .

    @ColeX - Do you have an equivalent that prints a WebView's content (including handling multiple printer pages where the webpage in the WebView has lots of content) for UWP? The sample that you referenced does not handle UWP at all (there are other issues with it too).

Sign In or Register to comment.