简体   繁体   English

使用curl命令将Java WebApp配置为通过http传输数据

[英]Configuring java webapp to transfer data via http using curl command

I'm currently running a webapp on tomcat 8 and wish to show the receive the output of a certain post request on the terminal screen without rendering a webpage. 我目前正在tomcat 8上运行一个webapp,并希望在终端屏幕上显示接收某个特定发布请求的输出而无需呈现网页。 As an example, I'm looking to use a command similar to this: 例如,我正在寻找使用类似于以下的命令:

curl -d "param1=xxx&param2=yyy" localhost:8080/webapp

and get a (short) response similar to this: 并得到类似于以下内容的(简短)响应:

"some data here"

I do not want to get the HTML of a webpage that would be rendered if I hit the URL in a browser. 我不想获取如果我在浏览器中单击URL就会呈现的网页的HTML。 Is there any way that this can be done in Tomcat? 有什么办法可以在Tomcat中完成吗?

AFAIK response content is generated in Tomcat by the web application (servlet, JSP or whatever). AFAIK响应内容是由Web应用程序(servlet,JSP或其他)在Tomcat中生成的。 You probably should create such a webapp which will output just a text without a tags and other html stuff, fe create a index.jsp file in tomcat webapps root directory (usually tomcat_root\\webapps\\ROOT ) with this content (yes, just one short line): 您可能应该创建这样的webapp,该webapp将仅输出不带标签和其他html内容的文本,例如,在tomcat webapps根目录(通常为tomcat_root \\ webapps \\ ROOT )中创建带有此内容的index.jsp文件(是,仅需短短一段线):

<%="some data here"%>

You'll get raw data by this request, BUT this will anyway a HTTP response. 您将通过此请求获取原始数据,但是无论如何这将是HTTP响应。 If you plan to deal with this data on lower level (say TCP), you'll get from server extra data like headers values etc. 如果您打算在较低级别(例如TCP)上处理此数据,则将从服务器获得额外的数据,例如标头值等。

Additionally you probably have to care about setting correct headers in response, fe: ContentType: text/plain , if your client software cares about it. 另外,如果客户端软件关心的话,您可能必须关心设置正确的响应头,例如fe: ContentType: text/plain Or just skip it: you'll get raw text content anyway independently of content-type headers values. 或跳过它:无论如何,您都将获得原始文本内容,而与内容类型标题值无关。

您必须指定该请求是POST请求

$ curl -i -X POST  -d "param1=xxx&param2=yyy" localhost:8080/webapp

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

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