简体   繁体   English

如何将数据从一个Web应用程序发送到另一个

[英]How to send data from one webApplication to another

我在两个不同的服务器上有两个Web应用程序。我想在标头或请求中向其他Web应用程序发送一些数据,我该怎么做,请帮帮我。

You can pass data by many means: 您可以通过多种方式传递数据:

  1. by making http request from your app: 通过从您的应用发出http请求:

     URLConnection conn = new URL("your other web app servlet url").openConnection(); // pass data using conn. Then on other side you can have a servlet that will receive these calls. 
  2. By using JMS for asynchronous communication. 通过使用JMS进行异步通信。

  3. By using webservice (SOAP or REST) 通过使用Web服务(SOAP或REST)

  4. By using RMI 通过使用RMI

  5. By sharing database between the apps. 通过在应用之间共享数据库。 So one writes to a table and the other reads from that table 因此,一个写入表,另一个从该表读取

  6. By sharing file system file(s)...one writes to a file the other reads from a file. 通过共享文件系统文件...一个写入文件,另一个从文件读取。

  7. You can use socket connection. 您可以使用套接字连接。

HttpClient can help HttpClient可以帮助

http://hc.apache.org/index.html http://hc.apache.org/index.html

Apache HttpComponents Apache HttpComponents

The Apache HttpComponents™ project is responsible for creating and maintaining a toolset of low level Java components focused on HTTP and associated protocols. Apache HttpComponents™项目负责创建和维护专注于HTTP和相关协议的低级Java组件的工具集。

One web application is functioning as the client of the other. 一个Web应用程序充当另一个Web应用程序的客户端。 You can use the org.apache.http library to create your HTTP client code in Java. 您可以使用org.apache.http库在Java中创建HTTP客户端代码。 How you will do this depends on a couple of things: 您将如何执行此操作取决于几件事:

  1. Are you using http or https? 您使用的是http还是https?
  2. Does the application you are sending data to have a REST API? 您发送数据的应用程序是否具有REST API?
  3. Do you have a SOAP based web service? 您有基于SOAP的Web服务吗?

If you have a SOAP based web service, then creating a Java client for it is very easy. 如果您具有基于SOAP的Web服务,那么为其创建Java客户端非常容易。 If not, you could do something like this and test the code in a regular Java client before trying to run it in the web application. 如果没有,您可以执行以下操作并在常规Java客户端中测试代码,然后再尝试在Web应用程序中运行它。

 import org.apache.http.client.utils.*;
 import org.apache.http.*; 
 import org.apache.http.impl.client.*;

  HttpClient httpclient = new DefaultHttpClient();
  try {
     URIBuilder builder = new URIBuilder();
     builder.setHost("yoursite.com").setPath(/appath/rsc/);
     builder.addParameter("user", username);
     builder.addParameter("param1", "SomeData-sentAsParameter");
     URI uri = builder.build();
     HttpGet httpget = new HttpGet(uri);
     HttpResponse response = httpclient.execute(httpget);
     System.out.println(response.getStatusLine().toString());
     if (response.getStatusLine().getStatusCode() == 200) {
        String responseText = EntityUtils.toString(response.getEntity());
        httpclient.getConnectionManager().shutdown();
     } else {
        log(Level.SEVERE, "Server returned HTTP code "
                + response.getStatusLine().getStatusCode());
     }
  } catch (java.net.URISyntaxException bad) {
     System.out.println("URI construction error: " + bad.toString());
  }

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

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