URLConnection call interface, urlconnection call
Preface:
The project is a java web, jdk1.4, weblogic 7, and other. net system, using the interface developed by wcf. The other party provides the interface url address and description for calling in post mode, and directly returns the json array (data in json array format) without passing parameters)
There are many methods to call interfaces, such as framework call, ajax call, And urlConnection. the project needs to remotely call the other party.. net system interfaces, which are developed using wcf. Then, the system on our side is java and the jdk is 1.4. Therefore, some mainstream frameworks cannot be called, jdk1.6 or later is required. Here, axis can be applied to jdk1.4. However, when using the client to generate code, the classes I generate are missing. If you don't know why, give up; then, because the cross-origin interface is called (that is, the system of both parties is not in the same server) and the post method is required for access, the ajax method does not work either, because ajax does not support cross-origin, of course, jsonp can be used for cross-origin access, but it is accessed in get mode. After struggling for two or three days, I asked for help. The Great God told me to use httpurlconnection to access the interface, that is, URLConnection to call the interface. Later, I took out the video notes of the webservice of Chuanzhi podcast, which also mentioned using URLConnection to call the interface, so I plan to try again. Success... It's so powerful... (It seems that this method can be used to call various interfaces or call webservice. You can try it. Don't be afraid of any problems, haha .....)
1. preparations:
For the URLConnection call interface, it seems that no related jar packages need to be imported. This depends on the project. Here, the returned string is a json array, so here I will use the json-related jar to obtain it. Import the json jar package.
Code: You can first write a main java program to connect and test the program, and then put it in a specific project for calling.
Public static void main (String [] args) {// create a url here. The url here is garbled. You can fill in your own interface to call. URL = new url ("http: // 10.10.10.83/GetProjectNameService. svc/getProjectName "); // open the connection URLConnection conn = url. openConnection (); // convert to HttpURL HttpURLConnection httpConn =. setProperty ("sun.net. client. defaultConnectTimeout "," 30000 "); System. setProperty ("sun.net. client. defaultReadTimeout "," 30000 "); // enable the input/output switch httpConn. setDoInput (true); httpConn. setDoOutput (true); // There cannot be cache httpConn for post submission. setUseCaches (false); // set that the transmitted content type is a serializable java object. If you do not need to pass parameters, you do not need to write it. // httpConn. setRequestProperty ("Content-type", "application/x-java-serialized-object"); // httpConn. setRequestProperty ("Content-type", "UTF-8"); // set the request header information where the request type is determined by the parameters you need to pass. // httpConn. setRequestProperty ("Content-type", "text/xml; charset = UTF-8"); // sets the Request Method httpConn. setRequestMethod ("POST"); // obtain the output stream OutputStream out = httpConn. getOutputStream (); // The ObjectOutputStream objOut = new ObjectOutputStream (out); objOut. writeObject (new String (""); objOut. flush (); objOut. close (); // The connect () method is executed by default when the input stream is obtained here. Therefore, the method InputStream in = httpConn can be omitted in the previous step. getInputStream (); // judge successful request StringBuffer sb = new StringBuffer (); JSONArray jarr = null; String str = ""; if (httpConn. getResponseCode () == 200 ){
// Using the buffer of the input stream here adding the UTF-8 can solve the problem of garbled BufferedReader = new BufferedReader (new InputStreamReader (in, "UTF-8"); String line = null; // read the input stream while (line = reader. readLine ())! = Null) {sb. append (line);} str = sb. toString (); System. out. println ("returned result:" + str ); // The result returned by the other party is a string in the json array format. Convert the string in the json array format into a json array. // you can convert the string to the desired type based on your project needs. The specific method can be Baidu jarr. = new JSONArray (str ); // print for (int I = 0; I <jarr. length (); I ++) {System. out. println (jarr. getString (I ));}}}