1 2 Previous Next 21 Replies Latest reply: Feb 20, 2013 9:46 AM by 977030 RSS

    Call Web Service method through a Proxy

    977030
      Hi everybody,
      I am using Java in Netbeans.
      I have implemented a client for a WS from the WSDL file.
      I have managed to call operations of the WS by coding:
      TheService service = new TheService();
      TheServiceSoap port = service.getTheServiceSoap();
      port.invokeMethod(parameters...);

      My problem is that I want to invoke WS operationg through a proxy and I do
      not want to use System.setProperty(... , ... ) as it sets the vm properties and I dont want that.

      Any suggestions, guidance is appreciated..
      Kind regards
      Dimitris
        • 1. Re: Call Web Service method through a Proxy
          jtahlborn
          http://stackoverflow.com/questions/6447127/how-can-i-use-an-http-proxy-for-a-jax-ws-request-without-setting-a-system-wide-p
          • 2. Re: Call Web Service method through a Proxy
            977030
            Thanks for the quick answer,
            Do you know how to implement Femi's solution that is described in the link you sent me? (the relpy that is marked as an answer)
            His instructions are not very clear
            • 3. Re: Call Web Service method through a Proxy
              jtahlborn
              974027 wrote:
              Thanks for the quick answer,
              Do you know how to implement Femi's solution that is described in the link you sent me? (the relpy that is marked as an answer)
              His instructions are not very clear
              no, i don't. and, poking at it a little bit, i'm guessing it's not trivial (maybe i'm missing some obvious tie between a SocketFactory and a Proxy...).

              anyway, thinking about it some more, it should be pretty straightforward to just setup a default ProxySelector for the jvm. it would return the relevant proxy info for the ws, and no proxy for all other urls.
              • 4. Re: Call Web Service method through a Proxy
                977030
                I have been trying to implement a ProxySelector but i was getting too many errors so I tried doing this as an alternative :
                (connecting to the webservice using HttpURLConnection and POST method)
                My code:
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(theHost, thePort));
                URL url = new URL(theUrl);
                HttpURLConnection con = (HttpURLConnection)url.openConnection(proxy);
                con.setRequestMethod("POST");
                con.setRequestProperty("Content-type", "text/xml; charset=utf-8");
                con.setDoOutput(true);
                con.setRequestProperty("SOAPAction", theUrl);
                OutputStream reqStream = con.getOutputStream();

                But at this line i get:
                Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request"

                ANy ideas? Can I use POST method to invoke a WS operation?
                Thanks again
                • 5. Re: Call Web Service method through a Proxy
                  977030
                  If you have any experience in using ProxySelector:
                  I implemented MyProxySelector and set it up to return a proxy of my choice when accessing a URI of my choice (in select() method).

                  The weird thing is that select is called twice at first with URI.getPort() = -1 (connection fails)
                  and then automatically select() is called again with the correct port but again connection fails

                  ANy ideas or guidance is appreciated
                  Dimitris
                  • 6. Re: Call Web Service method through a Proxy
                    jtahlborn
                    can you get the proxy to work when you set the proxy System property for the entire jvm?
                    • 7. Re: Call Web Service method through a Proxy
                      977030
                      My code is :
                      System.setProperty("http.proxyHost", theHost);
                      System.setProperty("http.proxyPort", "8008");

                      port.web_service_Operation(...parameters... .);

                      System.clearProperty("proxySet");
                      System.clearProperty("http.proxyHost");
                      System.clearProperty("http.proxyPort");

                      And with that^ the operation is invoked.
                      But I didn't really check if it is invoked through proxy as this is NOT the solution i want....
                      • 8. Re: Call Web Service method through a Proxy
                        jmsjr
                        Not sure what you are missing, but selecting a proxy WITHOUT using a system property is described in the TechNotes:

                        http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html
                        • 9. Re: Call Web Service method through a Proxy
                          jmsjr
                          974027 wrote:
                          I have been trying to implement a ProxySelector but i was getting too many errors so I tried doing this as an alternative :
                          (connecting to the webservice using HttpURLConnection and POST method)
                          My code:
                          Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(theHost, thePort));
                          URL url = new URL(theUrl);
                          HttpURLConnection con = (HttpURLConnection)url.openConnection(proxy);
                          con.setRequestMethod("POST");
                          con.setRequestProperty("Content-type", "text/xml; charset=utf-8");
                          con.setDoOutput(true);
                          con.setRequestProperty("SOAPAction", theUrl);
                          OutputStream reqStream = con.getOutputStream();

                          But at this line i get:
                          Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request"

                          ANy ideas? Can I use POST method to invoke a WS operation?
                          Thanks again
                          Probably best if you see / show us the HTTP traffic using a sniffer ( wireshark or something similar ).
                          • 10. Re: Call Web Service method through a Proxy
                            977030
                            I am still having this problem,
                            Can't anyone help?
                            I have managed to make ProxySelector work BUT the admin in the office disapproves (something about thread-safety) so I want a totally different way.

                            I tried:
                            BindingProvider provider = (BindingProvider)port;
                            provider.getRequestContext().put("http.proxyHost", (scriptContext.getProxyHostHR()).toString());
                            provider.getRequestContext().put("http.proxyPort", (scriptContext.getProxyPortHR()).toString());

                            but this ^ throws exception about unknownHost, it is caused because the proxy is not used.

                            Any ideas?
                            • 11. Re: Call Web Service method through a Proxy
                              jtahlborn
                              974027 wrote:
                              I am still having this problem,
                              Can't anyone help?
                              I have managed to make ProxySelector work BUT the admin in the office disapproves (something about thread-safety) so I want a totally different way.
                              huh? if you have a working solution, why don't you just resolve the "thread-safety" problem, whatever that is?
                              • 12. Re: Call Web Service method through a Proxy
                                977030
                                The solution of the Proxyselector can be used in my case only for a local standalone client.
                                It doesn't work when it is used in the application i want it to work
                                • 13. Re: Call Web Service method through a Proxy
                                  jtahlborn
                                  974027 wrote:
                                  The solution of the Proxyselector can be used in my case only for a local standalone client.
                                  It doesn't work when it is used in the application i want it to work
                                  why?
                                  • 14. Re: Call Web Service method through a Proxy
                                    EJP
                                    System.clearProperty("proxySet");
                                    Contrary to what you will read in certain books and web pages (not here), there is no such property as 'proxySet'. Setting it, clearing it, setting it to 'true', setting it to 'false', all accomplish exactly nothing.
                                    1 2 Previous Next