spring 如何使用spring发送HTTP post请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17423854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send HTTP post request using spring
提问by Basit
What configuration do i need to send HTTP post request through spring. I am using java application , it's not a web project. Can i use spring to send HTTP post request? I google it but most almost all examples are using spring MVC. Can i use just spring to send HTTP post request ?
我需要什么配置通过spring发送HTTP post请求。我正在使用 java 应用程序,它不是一个网络项目。我可以使用 spring 发送 HTTP post 请求吗?我用谷歌搜索,但几乎所有的例子都使用 spring MVC。我可以只使用 spring 发送 HTTP post 请求吗?
I found this bean on net but I don't know what to do after it. I am using spring3.2 and this post i think is of year 2008...
我在网上找到了这个豆子,但我不知道之后该怎么做。我正在使用 spring3.2,我认为这篇文章是 2008 年的...
<bean id="httpClient" class="org.springbyexample.httpclient.HttpClientTemplate">
<property name="defaultUri">
<value><![CDATA[http://localhost:8093/test]]></value>
</property>
</bean>
Any suggestions?
有什么建议?
回答by Dhanush Gopinath
If you are using Spring 3.0+, it will be better to use RestTemplatefor sending HTTP requests. Once you wire the RestTemplate(API), you can use different methods in it to send different types of HTTP requests.
如果您使用的是 Spring 3.0+,最好RestTemplate用于发送 HTTP 请求。一旦您连接了RestTemplate( API),您就可以在其中使用不同的方法来发送不同类型的 HTTP 请求。
回答by gerrytan
You don't need Spring just to issue a HTTP post, see this post: Using java.net.URLConnection to fire and handle HTTP requests
您不需要 Spring 来发布 HTTP 帖子,请参阅此帖子:使用 java.net.URLConnection 来触发和处理 HTTP 请求
And yes you can use Spring in a non-web application / command line application. Just create an instance of ApplicationContext (eg: ClassPathApplicationContext with path to your beans xml configuration injected)
是的,您可以在非 Web 应用程序/命令行应用程序中使用 Spring。只需创建一个 ApplicationContext 的实例(例如:ClassPathApplicationContext 注入了你的 bean xml 配置的路径)

