0

I have two web servers (call them A and B), one with a ASP.NET web API (A) and the other serves a web page (B).

How do I get around the cross-domain issue ? I serve the web page from B and I run some Java servlets at that server. I also want to access database content on server A which has a ASP.NET web API available to get at it.

I could open a TCP/IP socket between the two servers to share info, but since I have a web API, how can I connect during the doGet() in the the Java Servlet to the web API to get the information ?

Any help would be greatly appreciated.

bluesky
  • 150
  • 1
  • 1
  • 9
  • 1
    I think the answer is to send a HTTP request from the java servlet by using a URLConnection as explained in this link... http://stackoverflow.com/questions/1359689/how-to-send-http-request-in-java I'm still interested in comments on this approach or alternatives. – bluesky Apr 04 '14 at 19:12
  • 1
    What's preventing your server B accessing the database directly on server A? Not adding an extra layer in the middle would make the task much easier. – Mardoz Apr 04 '14 at 19:21
  • @Martin, good question. There are actually many server Bs remotely accessing over the Internet, and even more browser clients, so the ASP.NET web API layer around the database helps with security and stability. There is also a separate admin web portal that is hosted on server A with different functions built with ASP.NET MVC. – bluesky Apr 04 '14 at 20:20
  • Your first comment is the answer. You're not going to get any better suggestions, not without explaining what you're actually doing. Connecting from a webpage to a vague API...you answered how already. – developerwjk Apr 04 '14 at 22:12
  • @developerwjk, yeah, I found URLConnection later. Thanks for the confirmation. I tried it and it seems to work. I am still interested in any comments from anyone. – bluesky Apr 05 '14 at 03:12
  • What I meant was if you're looking for something other than URLConnection more specifics as to what the API is doing might get better responses. – developerwjk Apr 07 '14 at 17:02

2 Answers2

0

Try this

try {
    URL url = new URL("server url");
    //This will open the connection
    URLConnection conn = url.openConnection();
    //connection is open now you can exchange your piece of information
    /*
    * YOUR INFO EXCHANGING CODE
    * 
    */
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
0

To allow access to a database for many clients through an internet facing web server the current most popular method is to create a service layer that makes a REST API available.

You may find tutorials such as this helpful for implementing this in ASP.NET: http://www.codeproject.com/Articles/426769/Creating-a-REST-service-using-ASP-NET-Web-API

Once you have made the information available you can make simple HTTP requests to your REST service and all clients should be able to reach this information. You've answered yourself on how to do this: How to send HTTP request in java?

Community
  • 1
  • 1
Mardoz
  • 1,597
  • 1
  • 12
  • 24