可以访问网站但不能访问子目录中的文本文件

发布于 2024-12-11 14:49:11 字数 731 浏览 0 评论 0原文

我是套接字编程的新手,正在尝试读取嵌套在网站目录中的在线 dat 文件。我可以连接到主网站,但不能连接到其任何子目录或我尝试在其中之一访问的文本文件。

即我可以连接到 www.mainsite.com,但不能连接到 www.mainsite.com/dir1/dir2/textfile.dat。我想使用 recv 或 read 来读取文本文件,并且我不介意以不同的方式访问它,例如 fdopen 等...

我正在 Windows 7 机器上使用 cygwin 在 gnu c 中编写此文件。

为了保护隐私,我将使用匿名网站名称。我正在使用的部分代码发布在下面 - 谢谢:

  if(getaddrinfo("www.mainwebsite.com","80"    /*this works as it stands and fails if  I use  
                  , &hints, &res)!=0)       www.mainwebsite.com/subdir/textfile.dat  */
  .......
  .......
  /*create new socket*/
int newsocket = socket( res->ai_family, res->ai_socktype, 0);
.....
int newconnect = connect(newsocket, res->ai_addr, res->ai_addrlen)
.....

I'm a newbie to socket programming and am trying to read an online dat file that is nested in a website directory. I can connect to the main website but not any of its subdirectories or the text file I am trying to access in one of them.

ie I can connect to www.mainsite.com but not www.mainsite.com/dir1/dir2/textfile.dat. I would like to read the text file using recv or read, and I don't mind accessing it a different way like say fdopen ect...

I am writing this in gnu c with cygwin on a windows 7 machine.

In the interests of privacy I'll use an anonymous website name. Parts of the code I am using is posted below -thank you:

  if(getaddrinfo("www.mainwebsite.com","80"    /*this works as it stands and fails if  I use  
                  , &hints, &res)!=0)       www.mainwebsite.com/subdir/textfile.dat  */
  .......
  .......
  /*create new socket*/
int newsocket = socket( res->ai_family, res->ai_socktype, 0);
.....
int newconnect = connect(newsocket, res->ai_addr, res->ai_addrlen)
.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗恋未遂 2024-12-18 14:49:11

制作 Web 客户端比仅仅连接到目录要复杂得多。连接到 www.site.com 后,您需要为所需文件编写一个 HTTP GET 请求,然后读取响应(应包括文件的内容)。

GET 请求的文本将如下所示:

const char *req = "GET /subdir/textfile.dat HTTP/1.1\r\nHost: www.site.com\r\nUser-Agent: myprogram\r\n\r\n";
write(newsocket, req, strlen(req));

这是一个超文本传输​​协议 (HTTP) 请求。如果您真的想了解 HTTP 的工作原理,还有很多东西需要学习。从 Wikipedia 页面开始,然后从那里分支出来......

如果您对了解 HTTP 的工作原理不太感兴趣,您可以查看 cURL 这是一个非常流行的库,它实现了 HTTP,让那些想要编写简单客户端而不涉及丑陋细节的人(像你一样)的生活变得更加轻松。

Making a web client is a lot more complicated than just connecting to the directory. Once you connect to www.site.com you'll need to write an HTTP GET request for the file you want, and then read the response (which should include the content of the file).

The text of the GET request will look something like this:

const char *req = "GET /subdir/textfile.dat HTTP/1.1\r\nHost: www.site.com\r\nUser-Agent: myprogram\r\n\r\n";
write(newsocket, req, strlen(req));

This is a Hyper Text Transfer Protocol (HTTP) request. There's a lot more to learn if you really want to know how HTTP works. Start with the Wikipedia pages and then branch out from there....

If you're less interested in understanding exactly how HTTP works, you can look into cURL which is a very popular library which implements HTTP, making life a lot easier for people (like you) who want to write simple clients without getting into the ugly details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文