Home > Java > javaTutorial > body text

Share URLConnection calling interface instance

零下一度
Release: 2017-07-18 09:54:28
Original
1239 people have browsed it

This article mainly introduces the Java background calling HttpURLConnection class to simulate browser request instances. This instance can be used for interface calls and has certain practical value. Friends in need You can refer to the following

Generally, it is inevitable to encounter calls to external interfaces during project development. This article describes Java's examples. The method of calling the HttpURLConnection class in the background to simulate browser requests can be used for interface calls and is shared with everyone for your reference

Write in front:

  The project is java web, jdk1.4, weblogic 7; the other party's .net system, and the interface developed with wcf. The other party provides the interface url address and instructions to call it in the post method. There is no need to pass parameters, and the json array (data in json array format) is returned directly.

There are many ways to call the interface, such as framework call, ajax call, and urlConnection. Wait. Due to the needs of the project, the interface of the other party's .net system needs to be called remotely. The interface is developed with wcf, and our system here is java, and jdk is 1.4, so some mainstream framework calls can no longer be used. Generally, jdk1.6 or above is required. Speaking of which, axis can be applied to jdk1.4, but when using the client-side code generation method, the classes I generated were missing. I don’t know why, so I gave up decisively; and because it is a cross-domain calling interface (also That is, the systems of both parties are not in the same server), and the post method is required to access, so the ajax method will not work, because ajax does not support cross-domain. Of course, jsonp can be used to cross-domain, but it is accessed in the get method. After struggling and trying for two or three days, I asked the master of Tieba for help. The master spoke to me and asked me to access it using httpurlconnection, which is the URLConnection calling interface. After that, I took out the video notes of Chuanzhi Podcast's webservice. It also mentioned the use of URLConnection calling interface, so I planned to try it. It worked...it's so powerful. . . (It seems that this method can be applied to calling various interfaces, and can also call webservice. Everyone can give it a try. Don’t be afraid if you encounter problems, hahaha...)

 

 1. Preparation work:

It seems that there is no need to import any related jar packages for the URLConnection calling interface. This depends on the project. What is returned here is the string of the json array, so here I need to use the json-related jar to obtain it. Just import the json jar package.

Code: You can first write a main java program to connect and test it, and then put it into a specific project to call it.

  URL url =  URL("http://10.10.10.83/GetProjectNameService/GetProjectNameService.svc/getProjectName"URLConnection conn =HttpURLConnection httpConn =System.setProperty("sun.net.client.defaultConnectTimeout", "30000""sun.net.client.defaultReadTimeout", "30000"httpConn.setDoInput(httpConn.setUseCaches(httpConn.setRequestMethod("POST"OutputStream out =ObjectOutputStream objOut =  String(""InputStream in =StringBuffer sb = = = ""(httpConn.getResponseCode() == 200BufferedReader reader =  BufferedReader( InputStreamReader(in,"UTF-8"= ((line = reader.readLine()) != ="返回结果:"+jarr = ( i=0;i<jarr.length();i++
Copy after login


The above is the detailed content of Share URLConnection calling interface instance. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!