Home>

I am a beginner.
When I tried to download xml data with subtitles in You Tube written in C #, I got an exception.
So, in order to find out the cause of the error,
A similar exception was thrown.
I would like to be able to download Youtube subtitle files in the end.
In this question, I would like to realize "download HTML" for the time being.

using System.IO;
using System.Net;
using System.Windows.Forms;
using System.Xml;
namespace YoutubeSubtitlesProject
{
    public partial class Form1: Form
    {
        public Form1 ()
        {
            InitializeComponent ();
            System.Net.WebClient wc = new System.Net.WebClient ();
            wc.DownloadString ("https://www.google.com/");// exception here
            wc.Dispose ();
        }
    }
}
Error message
System.Net.WebException
  HResult = 0x80131509
  Message = Cannot connect to the remote server.
  Source = System
  Stack trace:
   Location System.Net.WebClient.DownloadDataInternal (Uri address, WebRequest&request)
   Location System.Net.WebClient.DownloadString (Uri address)
   Location System.Net.WebClient.DownloadString (String address)
   Location YoutubeSubtitlesProject.Form1..ctor () (C: \ Users \ skytomo \ source \ repos \ YoutubeSubtitlesProject \ YoutubeSubtitlesProject \ Form1.cs): Line 20
   Location YoutubeSubtitlesProject.Program.Main () (C: \ Users \ skytomo \ source \ repos \ YoutubeSubtitlesProject \ YoutubeSubtitlesProject \ Program.cs): Line 19
Internal exception 1:
SocketException: An attempt was made to access a socket in a way forbidden by its permissions. 172.217.25.99:443
Applicable source code
wc.DownloadString ("https://www.google.com/");
Tried

I tried with Yahoo, but the same result.
Also, I tried to drop the image (jpg), but it was not good for the same reason.
I googled various things and didn't understand why these exceptions were thrown.
(Correction) It seems that there is a possibility that the other server will be rejected if it is a super major site,
I tried again with C language (https://9cguide.appspot.com/) that I remember hardly, but
Anything throws an exception: "Attempted to access socket in a way forbidden by permissions".

Supplemental information

OS: Windows10
Internet environment: Wi-Fi
Integrated development environment: Visual Studio 2017
Target net framework: .NET Framework 4.6.1

WebBrowser doesn't work either.

Thanks for your answer.
I tried using WebBrowser, but the result is
"Navigation to the web page has been cancelled"
Has been displayed.
"Update the page with the latest information."
Click
"This page cannot be displayed"
"Please make sure the web address https://StackOverflow.com is correct"
Is displayed.
"Search this site with Bing"
Click
"This page cannot be displayed"
"Please make sure the web address https://www.bing.com is correct"
Has been displayed. (That ’s not even Bing.)
By the way, the local HTML file is displayed properly.

using System;
using System.Windows.Forms;
namespace WebBrowserProject
{
    public partial class Form1: Form
    {
        public Form1 ()
        {
            InitializeComponent ();
            webBrowser1.Navigate (@ " https://www.tutorialfor.com/go.php?id=30410 ");
            Console.WriteLine (webBrowser1.DocumentText);
        }
    }
}
  • Answer # 1

    https://stackoverflow.com/questions/25562434/sftp-in-c-sharp-using-a-webclient

    I wonder why. Does not support SSH connection.
    WebClient is a relic of the past, so use HttpClient.

    https://qiita.com/rawr/items/f78a3830d894042f891b

    Since Google is taking bot measures, the difficulty level is high and the API can be used.

    Added.
    If you think about JS, it ’s best to use Headless Chrome.
    https://qiita.com/mifumi323/items/296d449c5808f04f98c4

    WebBrowser has IE in its contents ...
    https://forest.watch.impress.co.jp/docs/news/1121672.html

  • Answer # 2

    JavaScript is used to dynamically acquire and display data such as images Even if a page is requested by WebClient or HttpClient, JavaScript does not work in WebClient or HttpClient, so nothing happens.

    I don't know how "xml data with subtitles written in You Tube" is rendered, but if it is dynamically acquired using JavaScript, It wo n’t do anything.

    Consider using WebBrowser. At least it is more likely than WebClient or HttpClient.

    Still, Google is no good. Using WebBrowser does nothing. An API is provided so you can only use it.

  • Answer # 3

    I'm very sorry.
    VirusbusterESET blocked access.
    It was solved by pressing "Unblock" from "Blocked application or device" in "Troubleshooting wizard" of "Network protection".
    It ’s really a rudimentary mistake. I'm sorry.
    The YouTube subtitle file has now been downloaded.
    Thank you alg, SurferOnWww and kiichi54321 for their answers.
    By the way, although I was able to download the Youtube subtitle file, I confirmed that I could not access Bing or Google. If you need to access Google in the future, I would like to refer to SuferOnWww and kiichi54321's answers. Thank you very much.

  • Answer # 4

    Is it a TLS version?
    System.Net.WebClientshould use SSL3.0 and TLS1.0 by default, but it seems that many sites are disabled as of 2018.
    System.Net.WebClient wc = new System.Net.WebClient ();is preceded byServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;to use TLS1.2 What happens if I try to clarify that?

    using System.IO;
    using System.Net;
    using System.Windows.Forms;
    using System.Xml;
    namespace YoutubeSubtitlesProject
    {
        public partial class Form1: Form
        {
            public Form1 ()
            {
                InitializeComponent ();
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;// add this
                System.Net.WebClient wc = new System.Net.WebClient ();
                wc.DownloadString ("https://www.google.co.jp/");// Is there an exception here?
                wc.Dispose ();
            }
        }
    }

    (Added)
    From .NET 4.5, the default value seems to have changed to TLS1.0, TLS1.1, TLS1.2.

    https://blogs.technet.microsoft.com/jpieblog/2015/04/07/net-framework-tls1-1-1-2/

      

    (Added 2017/7/20)
      As described in the following public information, by applying the update described in this security advisory, the default value of the SecurityProtocol property is changed from SSL 3.0 and TLS 1.0 to TLS 1.0, TLS 1.1 and TLS in the .NET Framework 4.5 environment. Changed to 1.2.

    Since this answer may be omitted.