Sending SOAP messages using Proxy in Java

I need to know how to install a proxy and confirm that it works.

I made a test program as follows:

enter image description here

If you can specify the proxy server address and port number.

(I found the address and port at: http://www.freeproxylists.net/ )

The SOAP call looks like this when the "Use proxies" checkbox is checked:

        Socket socket = new Socket();
        SocketAddress sockaddr = new InetSocketAddress(PROXY_ADDRESS, PROXY_PORT);
        socket.connect(sockaddr, 10000);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(socket.getInetAddress(), PROXY_PORT));
        URL url = new URL(urlStr);
        HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
        return connection.call(message, uc);

The problem is that in the last line the SOAPConnection.call(..)dose does not allow input HttpURLConnectionand thus gives:

Bad endpoint type

Any idea how to add a proxy address to a SOAP call and verify that the proxy is in use?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URL;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;

public class TestProxy implements ActionListener {

    public JTextField proxyField;
    public JTextField portField;
    public JCheckBox useProxy;

    // GUI
    public TestProxy() {
        JFrame f = new JFrame("Proxy tester");
        f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));

        proxyField = new JTextField("103.247.43.218");
        portField = new JTextField("8081");
        useProxy = new JCheckBox("Use Proxy");

        JButton b = new JButton("Connect!");
        b.addActionListener(this);

        f.getContentPane().add(proxyField);
        f.getContentPane().add(portField);
        f.getContentPane().add(useProxy);
        f.getContentPane().add(b);

        f.pack();
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    // ACTION
    @Override
    public void actionPerformed(ActionEvent e) {

        SOAPMessage response = null;
        try {
            SOAPMessage msg = createSOAPRequest();
            String urlStr = "http://www.webservicex.net/CurrencyConvertor.asmx?WSDL";
            response = sendSOAPMessage(msg, urlStr);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (SOAPException e1) {
            e1.printStackTrace();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        if (response == null)
            JOptionPane.showMessageDialog(null, "Null returned...");
        else
            JOptionPane.showMessageDialog(null, "Returned response!!!");
    }

    // SOAP CALL
    public SOAPMessage sendSOAPMessage(SOAPMessage message, String urlStr) throws SOAPException, MalformedURLException {

        String PROXY_ADDRESS = proxyField.getText();
        int PROXY_PORT = Integer.parseInt(portField.getText());
        try {
            SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();
            SOAPConnection connection = factory.createConnection();
            if (useProxy.isSelected()) {
                Socket socket = new Socket();
                SocketAddress sockaddr = new InetSocketAddress(PROXY_ADDRESS, PROXY_PORT);
                socket.connect(sockaddr, 10000);
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(socket.getInetAddress(), PROXY_PORT));
                URL url = new URL(urlStr);
                HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
                // This "call" is not allowed!!
                return connection.call(message, uc);
            } else {
                return connection.call(message, urlStr);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    // SOAP MESSAGE
    private static SOAPMessage createSOAPRequest() throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        return soapMessage;
    }

    public static void main(String[] args) {
        new TestProxy();
    }
}
+4
source share
2 answers

sendSOAPMessage, :

public static SOAPMessage sendSOAPMessage(SOAPMessage message, String url, final Proxy p) throws SOAPException, MalformedURLException {
    SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();
    SOAPConnection connection = factory.createConnection();

    URL endpoint = new URL(null, url, new URLStreamHandler() {
        protected URLConnection openConnection(URL url) throws IOException {
            // The url is the parent of this stream handler, so must
            // create clone
            URL clone = new URL(url.toString());

            URLConnection connection = null;
            if (p.address().toString().equals("0.0.0.0/0.0.0.0:80")) {
                connection = clone.openConnection();
            } else
                connection = clone.openConnection(p);
            connection.setConnectTimeout(5 * 1000); // 5 sec
            connection.setReadTimeout(5 * 1000); // 5 sec
            // Custom header
            connection.addRequestProperty("Developer-Mood", "Happy");
            return connection;
        }
    });

    try {
        SOAPMessage response = connection.call(message, endpoint);
        connection.close();
        return response;
    } catch (Exception e) {
        // Re-try if the connection failed
        SOAPMessage response = connection.call(message, endpoint);
        connection.close();
        return response;
    }
}
+6

Java Proxy - . , , Proxy reachable . , URL ( HTTP, , - , SOAP) EDIT:

SocketAddress,

   Socket socket = null;
        try {
            SocketAddress sockaddr = new InetSocketAddress(PROXY_ADDRESS,
                    PROXY_PORT);

            socket = new Socket();

            socket.connect(sockaddr, 10000);
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
                    socket.getInetAddress(), PROXY_PORT));

            if (socket.getInetAddress().isReachable(10000)) {
                URL url = new URL("http://www.popofibo.com");
                HttpURLConnection uc = (HttpURLConnection) url
                        .openConnection(proxy);
                System.out.println("Content: " + uc.getContentType());
                uc.connect();
            }

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

- , InetAddress . , - http-.

: isReachable() , boolean, .

Proxy, Socket . -, , IP, .

public static void main(String[] args) {
        Socket socket = new Socket();
        boolean proxyReachable = false;

        SocketAddress sockaddr = new InetSocketAddress(PROXY_ADDRESS,
                PROXY_PORT);

        try {
            socket.connect(sockaddr, 10000);
            proxyReachable = true;
        } catch (SocketTimeoutException e) {
            proxyReachable = false;

        } catch (IOException e) {
            proxyReachable = false;
        }

        if (proxyReachable) {
            try {
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
                        socket.getInetAddress(), PROXY_PORT));

                URL url = new URL("http://www.popofibo.com");

                HttpURLConnection uc = (HttpURLConnection) url
                        .openConnection(proxy);
                System.out.println("Content: " + uc.getContentType());
                uc.connect();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

( / ):

Content: text/html; charset=UTF-8
0

Source: https://habr.com/ru/post/1524049/


All Articles