java 我可以将 Web 服务作为 HTTP 请求调用吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11417166/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:02:08  来源:igfitidea点击:

can i call web service as a HTTP request?

javaweb-services

提问by priceline

I have some limitations in using generated stubs with the 3rd party software. SO, I am looking for other options like simple HTTP request and response to get the result. I will probably need to pass 5 or 6 parameters to one operation and get one output from the web service.

我在使用 3rd 方软件生成的存根方面有一些限制。所以,我正在寻找其他选项,比如简单的 HTTP 请求和响应来获得结果。我可能需要将 5 或 6 个参数传递给一项操作并从 Web 服务获得一项输出。

I can create a simple JSP file, which internally calls the webservice. I can call this JSP via HTTP Request. I want to check if there are any other options.

我可以创建一个简单的 JSP 文件,它在内部调用 webservice。我可以通过 HTTP 请求调用这个 JSP。我想检查是否还有其他选择。

I am using JDK1.6, JBoss 5.1.

我使用的是 JDK1.6、JBoss 5.1。

回答by Tomasz Nurkiewicz

SOAP web service requests are normal POST HTTP requests which you can trigger using any client, including simple URLConnectionor even curl. See: Sending a SOAP request to a Web Service via URLConnection.

SOAP Web 服务请求是普通的 POST HTTP 请求,您可以使用任何客户端触发它们,包括 simpleURLConnection甚至curl. 请参阅:通过 URLConnection 向 Web 服务发送 SOAP 请求

You don't need a JSP (in fact, calling external web services from JSP is a terrible idea from maintenance perspective). You can call web services from any Java code, even directly from mainmethod.

您不需要 JSP(实际上,从维护的角度来看,从 JSP 调用外部 Web 服务是一个糟糕的主意)。您可以从任何 Java 代码调用 Web 服务,甚至可以直接从main方法调用。

回答by Kumar Vivek Mitra

Try this out...

试试这个...

public void postData() throws Exception {


 HttpClient client = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("https://www.xyz.com");

 List<NameValuePair> list = new ArrayList<NameValuePair>(1);

 list.add(new BasicNameValuePair("name","ABC");

 httppost.setEntity(new UrlEncodedFormEntity(list));

 HttpResponse r = client.execute(httppost);

}

回答by tbraun

I agree with Tomasz Nurkiewicz. Do not use a JSP to call the web service.

我同意 Tomasz Nurkiewicz 的观点。不要使用 JSP 来调用 Web 服务。

Instead, create a web service that calls the other web service you need. This way you can easily work with the result before sending back the response.

相反,创建一个 Web 服务来调用您需要的其他 Web 服务。通过这种方式,您可以在发回响应之前轻松处理结果。

Web services can be easily created on JBoss using annotations.

可以使用注释在 JBoss 上轻松创建 Web 服务。