可以访问网站但不能访问子目录中的文本文件
我是套接字编程的新手,正在尝试读取嵌套在网站目录中的在线 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
制作 Web 客户端比仅仅
连接
到目录要复杂得多。连接到www.site.com
后,您需要为所需文件编写
一个 HTTPGET
请求,然后读取
响应(应包括文件的内容)。GET
请求的文本将如下所示:这是一个超文本传输协议 (HTTP) 请求。如果您真的想了解 HTTP 的工作原理,还有很多东西需要学习。从 Wikipedia 页面开始,然后从那里分支出来......
如果您对了解 HTTP 的工作原理不太感兴趣,您可以查看 cURL 这是一个非常流行的库,它实现了 HTTP,让那些想要编写简单客户端而不涉及丑陋细节的人(像你一样)的生活变得更加轻松。
Making a web client is a lot more complicated than just
connect
ing to the directory. Once you connect towww.site.com
you'll need towrite
an HTTPGET
request for the file you want, and thenread
the response (which should include the content of the file).The text of the
GET
request will look something like this: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.