POST接收器(服务器)

编程入门 行业动态 更新时间:2024-10-28 20:24:04
本文介绍了POST接收器(服务器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我发现自己需要一种方法来测试帖子从我的申请中提出的请求。

I find myself in need of a way to test the post requests made from my application.

我将我的请求发送到 http://macbook-pro.local/ ,我正在试图弄清楚我可以运行什么服务来阅读和显示请求。

I'm sending my request to macbook-pro.local/, and I'm trying to figure out what service I can run to read and display the request.

我查看了 RestKit 没有任何运气。

I looked into RestKit without any luck.

使用 SignalR 可以工作,但端口 macos 未按预期工作。

Using SignalR could work, but the port to macos isn't working as expected.

推荐答案

你是什么want基本上是一个非常简单的Web服务器,但是如果你想要的只是打印出HTTP POST请求中的内容,你可以使用内置的'nc'命令。您可以使用终端在本地端口10000上打印传入请求的内容,方法是在这样的循环中运行'nc':

What you want is basically a very simple web server, but if all you want is printing out what comes in a HTTP POST request you can get away with using the built-in 'nc' command. You can use Terminal to print out the contents of incoming requests on local port 10000 by running 'nc' in a loop like this:

while true; do nc -l 10000 < /dev/null ; printf '\n\n\n'; done

然后你可以去 http:// localhost:10000 在您的浏览器中,并在您的终端窗口中看到HTTP请求。 Web浏览器将显示错误消息,因为'nc'不够智能回复。

You can then go to localhost:10000 in your browser and see the HTTP request appear in your Terminal window. The web browser will give an error message since 'nc' isn't smart enough to reply.

要测试HTTP POST请求,您可以使用'curl':

To test an HTTP POST request you can use 'curl':

curl --data "this-is-POST-data" localhost:10000

同样,curl会给出一条错误消息,因为'nc'只是关闭连接而没有给出正确的HTTP回复。您可以使用对此类所有请求的静态HTTP响应进行'nc'回复:

Again, curl will give an error message because 'nc' simply closes the connection without giving a proper HTTP reply. You can have 'nc' reply with a static HTTP response to all requests like this:

while true; do printf 'HTTP/1.0 200 OK\r\nContent-type: text-plain\r\n\r\nHello, world!' | nc -l 10000 ; printf '\n\n\n'; done

如果你需要使用端口80,你需要以root身份运行'nc'(例如使用'sudo')。

If you need to use port 80 you'll need to run 'nc' as root (e.g. using 'sudo').

但是,如果您需要进行任何类型的真实HTTP流量,则需要获得正确的Web服务器。 OS X附带Apache Web服务器,可以使用命令apachectl start(apachectl stop来启动它)来启动它。 CGI已启用,因此您可以将可执行文件放入/ Library / WebServer / CGI-Executables并使用 http:// localhost / cgi-bin / filename 访问它们。例如,如果您创建以下CGI脚本:

If you need to have any kind of real HTTP traffic going on, however, you will need to get a proper web server. OS X comes with the Apache web server which can be started with the command "apachectl start" ("apachectl stop" to stop it). CGI is enabled so you can put executables into /Library/WebServer/CGI-Executables and access them using localhost/cgi-bin/filename. For example, if you create the following CGI script:

#!/bin/sh printf 'Content-type: text/plain\r\n\r\n' cat > /tmp/post-data echo OK

称之为test.sh 并将其放在CGI-Executables文件夹中,然后运行:

call it, say, "test.sh" and place it in the CGI-Executables folder, and run:

chmod +x /Library/WebServer/CGI-Executables/test.sh

然后,每当你发送一个POST请求到 http:/ /localhost/cgi-bin/test.sh 它会将POST数据的内容保存到计算机上的文件/ tmp / post-data。

Then, whenever you send a POST request to localhost/cgi-bin/test.sh it will save the contents of the POST data to the file /tmp/post-data on your computer.

注意:在所有示例中,如果这是您的计算机主机名,则localhost可以替换为macbook-pro.local,以便通过网络进行访问。

Note: in all examples, "localhost" can be replaced with "macbook-pro.local" for accesses over the network if that is your computer hostname.

另请注意,您的OS X防火墙权限可能会阻止nc和其他软件收听TCP端口。通常你应该获得一个权限对话框,但如果你只是得到权限被拒绝,请在系统偏好设置 - >防火墙 - >防火墙选项中调整防火墙设置。

Also note that your OS X firewall permissions may block 'nc' and other software from listening to TCP ports. Usually you should get a permission dialog, but if you simply get "permission denied", tweak your firewall settings in System Preferences -> Firewall -> Firewall Options.

更多推荐

POST接收器(服务器)

本文发布于:2023-10-11 04:48:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1480730.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接收器   服务器   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!