URLConnection call interface

Write in front:

The project is java web, jdk1.4, weblogic 7; the other side. net system, the interface developed by wcf. The other party provides the interface url address, as well as the description of using post to call, without passing parameters, directly returns json array (data in json array format)

There are many methods to invoke interfaces, such as framework invocation, ajax invocation, urlConnection and so on. Because of project needs, we need to call the interface of the other side's. net system remotely. Its interface is developed with wcf. Then our system is java, and JDK is 1.4, so some mainstream framework invocation is no longer needed. Generally, it needs jdk1.6 or more. At this point, axis can be applied to jdk1.4, but when using the way of client-side code generation, the classes I generated are missing, I don't know why, and I give up resolutely. Then because it is a cross-domain call interface (that is, the systems of both sides are not in a server), and need post mode to access, the way of ajax is not good, because ajax does not support cross-domain. Of course, jsonp can be used across domains, but it is accessed in get mode. After struggling for two or three days, I asked for help and posted the god, God and voice with me, let me use httpurlconnection to access, that is, URLConnection call interface. Later, I took out the video notes of the webservice of the intellectual podcast, which also mentioned the use of URLConnection to invoke the interface, so I decided to try. Success... It's really powerful. (It seems that this method can be applied to various interface calls, can also call web service, everyone can try, do not be afraid of problems, aha ha.... )


1. Preparations:

For URLConnection invocation interfaces, it seems that there is no need to import any relevant jar packages, depending on the project. The string returned here is the JSON array, so I'm going to use the JSON related jar to get it here. Just import json's jar package.

Code: You can write a main java program to connect and test, and then put it on a specific project for invocation.


public static void main(String[] args) {
        //Establish url Address here url Address I'm scribbling. Just fill in the interface you need to call.
        URL url = new URL("");
        //open a connection
        URLConnection conn = url.openConnection();
        //convert to HttpURL
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        //Connection timeout related settings to see the relevant blog said that to add, there is no removal
        System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
        System.setProperty("sun.net.client.defaultReadTimeout", "30000");
        //Switch on input and output
        //post Submission cannot be cached
        //Setting the content type to be transmitted is serializable java Objects do not need to be written if they do not need to pass parameters
        //httpConn.setRequestProperty("Content-type", "application/x-java-serialized-object");
        //httpConn.setRequestProperty("Content-type", "UTF-8");
        //Setting the header information of the request    The type of request here is determined by the parameters that you need to pass.  
        //httpConn.setRequestProperty("Content-type", "text/xml;charset=UTF-8");
        //Setting Request Mode
        //Obtain the output stream   
        OutputStream out = httpConn.getOutputStream();
        //Send data even though no parameters need to be passed, just send data through empty strings.
        ObjectOutputStream objOut = new ObjectOutputStream(out);
        objOut.writeObject(new String(""));
        //The acquisition of the input stream is executed by default when the input stream is obtained here. connect()This method can be omitted from the previous step.
        InputStream in = httpConn.getInputStream();
        //Judge the success of the request
        StringBuffer sb = new StringBuffer();
        JSONArray jarr = null;
        String str = "";
        if(httpConn.getResponseCode() == 200){
//Add here using the buffer of the input stream UTF-8 Can solve the problem of random code BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8")); String line = null; //Read input stream while((line = reader.readLine()) != null){ sb.append(line); } str = sb.toString(); System.out.println("Returns the result:"+str); //Because the result returned by the other party is json Array format strings will json The string in array format is converted to json array //According to the needs of their own projects can be converted to the type of needs of specific methods can be Baidu jarr = new JSONArray(str); //Traversal printing for(int i=0;i<jarr.length();i++){ System.out.println(jarr.getString(i)); } } }

Tags: Java JSON Weblogic JDK

Posted on Sun, 16 Jun 2019 19:07:36 -0400 by Gregg