简体   繁体   English

使用 C# HttpWebRequest 向 TikTok 发送请求的问题

[英]Issue using C# HttpWebRequest to send request to TikTok

I seem to be having issues just making a basic request to the TikTok homepage using the HttpWebRequest module in C#.我似乎在使用 C# 中的 HttpWebRequest 模块向 TikTok 主页发出基本请求时遇到了问题。 Here is my code.这是我的代码。

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://tiktok.com");

        using (HttpWebResponse response = (HttpWebResponse)(request.GetResponse()))
        {
            Console.WriteLine("Done");
        }

When I do this, the program hangs for a while, then I get a System.Net.WebException detailed below.当我这样做时,程序会挂起一段时间,然后我会收到下面详细介绍的 System.Net.WebException。 I've tried setting user agents and timeouts but both don't seem to work.我试过设置用户代理和超时,但似乎都不起作用。 Any help on this would be appreciated.对此的任何帮助将不胜感激。

System.Net.WebException HResult=0x80131509 Message=The underlying connection was closed: An unexpected error occurred on a receive. System.Net.WebException HResult=0x80131509 Message=底层连接已关闭:接收时出现意外错误。 Source=System StackTrace: at System.Net.HttpWebRequest.GetResponse() in f:\dd\NDP\fx\src\net\System\Net\HttpWebRequest.cs:line 2293 at Live.Program.Main(String[] args) in E:\git\batch\live\Program.cs:line 733 Source=System StackTrace: 在 System.Net.HttpWebRequest.GetResponse() in f:\dd\NDP\fx\src\net\System\Net\HttpWebRequest.cs:line 2293 at Live.Program.Main(String[] args ) 在 E:\git\batch\live\Program.cs:line 733

This exception was originally thrown at this call stack: System.Net.Sockets.NetworkStream.Read(byte[], int, int) in NetworkStream.cs此异常最初是在此调用堆栈中引发的:NetworkStream.cs 中的 System.Net.Sockets.NetworkStream.Read(byte[], int, int)

Inner Exception 1: IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.内部异常1:IOException: Unable to read data from the transport connection:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机没有响应。

Inner Exception 2: SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond内部异常2:SocketException:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机没有响应

I'm not entirely sure why, but once you do我不完全确定为什么,但一旦你这样做

request.UserAgent=".NET Framework Test Client";

before getting the response, the problem goes away.在得到响应之前,问题就消失了。 I tried with different User-Agent headers but I always get the timeout.我尝试使用不同User-Agent标头,但总是超时。 Only .NET Framework Test Client works for me.只有.NET Framework Test Client适用于我。 I hope that helps you at least a bit.我希望这至少对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM