Home Java javaTutorial Share URLConnection calling interface instance

Share URLConnection calling interface instance

Jul 18, 2017 am 09:54 AM
urlconnection interface transfer

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
What are the internal interfaces of a computer motherboard? Recommended introduction to the internal interfaces of a computer motherboard What are the internal interfaces of a computer motherboard? Recommended introduction to the internal interfaces of a computer motherboard Mar 12, 2024 pm 04:34 PM

When we assemble the computer, although the installation process is simple, we often encounter problems in the wiring. Often, users mistakenly plug the power supply line of the CPU radiator into the SYS_FAN. Although the fan can rotate, it may not work when the computer is turned on. There will be an F1 error "CPUFanError", which also causes the CPU cooler to be unable to adjust the speed intelligently. Let's share the common knowledge about the CPU_FAN, SYS_FAN, CHA_FAN, and CPU_OPT interfaces on the computer motherboard. Popular science on the CPU_FAN, SYS_FAN, CHA_FAN, and CPU_OPT interfaces on the computer motherboard 1. CPU_FANCPU_FAN is a dedicated interface for the CPU radiator and works at 12V

Common programming paradigms and design patterns in Go language Common programming paradigms and design patterns in Go language Mar 04, 2024 pm 06:06 PM

As a modern and efficient programming language, Go language has rich programming paradigms and design patterns that can help developers write high-quality, maintainable code. This article will introduce common programming paradigms and design patterns in the Go language and provide specific code examples. 1. Object-oriented programming In the Go language, you can use structures and methods to implement object-oriented programming. By defining a structure and binding methods to the structure, the object-oriented features of data encapsulation and behavior binding can be achieved. packagemaini

Introduction to PHP interfaces and how to define them Introduction to PHP interfaces and how to define them Mar 23, 2024 am 09:00 AM

Introduction to PHP interface and how it is defined. PHP is an open source scripting language widely used in Web development. It is flexible, simple, and powerful. In PHP, an interface is a tool that defines common methods between multiple classes, achieving polymorphism and making code more flexible and reusable. This article will introduce the concept of PHP interfaces and how to define them, and provide specific code examples to demonstrate their usage. 1. PHP interface concept Interface plays an important role in object-oriented programming, defining the class application

Solution to NotImplementedError() Solution to NotImplementedError() Mar 01, 2024 pm 03:10 PM

The reason for the error is in python. The reason why NotImplementedError() is thrown in Tornado may be because an abstract method or interface is not implemented. These methods or interfaces are declared in the parent class but not implemented in the child class. Subclasses need to implement these methods or interfaces to work properly. How to solve this problem is to implement the abstract method or interface declared by the parent class in the child class. If you are using a class to inherit from another class and you see this error, you should implement all the abstract methods declared in the parent class in the child class. If you are using an interface and you see this error, you should implement all methods declared in the interface in the class that implements the interface. If you are not sure which

Application of interfaces and abstract classes in design patterns in Java Application of interfaces and abstract classes in design patterns in Java May 01, 2024 pm 06:33 PM

Interfaces and abstract classes are used in design patterns for decoupling and extensibility. Interfaces define method signatures, abstract classes provide partial implementation, and subclasses must implement unimplemented methods. In the strategy pattern, the interface is used to define the algorithm, and the abstract class or concrete class provides the implementation, allowing dynamic switching of algorithms. In the observer pattern, interfaces are used to define observer behavior, and abstract or concrete classes are used to subscribe and publish notifications. In the adapter pattern, interfaces are used to adapt existing classes. Abstract classes or concrete classes can implement compatible interfaces, allowing interaction with original code.

How matlab calls m files-matlab method of calling m files How matlab calls m files-matlab method of calling m files Mar 04, 2024 pm 01:49 PM

Many friends still don’t know how to call m files in matlab, so the editor below explains how to call m files in matlab. If you are in need, please take a look. I believe it will be helpful to everyone. 1. First open the matlab software and click "Open" in the main interface, as shown in the figure below. 2. Then select an m file that needs to be opened and select Open, as shown in the figure below. 3. Then look at the file name and number of variables of the m file in the editor, as shown in the figure below. 4. You can enter the m file name followed by the variable value in brackets on the command line to call it, as shown in the figure below. 5. Finally, the m file can be successfully called, as shown in the figure below. The above is the complete description of how to call m files in matlab brought to you by the editor.

What is the difference between interfaces and abstract classes in PHP? What is the difference between interfaces and abstract classes in PHP? Jun 04, 2024 am 09:17 AM

Interfaces and abstract classes are used to create extensible PHP code, and there is the following key difference between them: Interfaces enforce through implementation, while abstract classes enforce through inheritance. Interfaces cannot contain concrete methods, while abstract classes can. A class can implement multiple interfaces, but can only inherit from one abstract class. Interfaces cannot be instantiated, but abstract classes can.

Insight into Hongmeng system: actual function measurement and usage experience Insight into Hongmeng system: actual function measurement and usage experience Mar 23, 2024 am 10:45 AM

As a new operating system launched by Huawei, Hongmeng system has caused quite a stir in the industry. As a new attempt by Huawei after the US ban, Hongmeng system has high hopes and expectations. Recently, I was fortunate enough to get a Huawei mobile phone equipped with Hongmeng system. After a period of use and actual testing, I will share some functional testing and usage experience of Hongmeng system. First, let’s take a look at the interface and functions of Hongmeng system. The Hongmeng system adopts Huawei's own design style as a whole, which is simple, clear and smooth in operation. On the desktop, various

See all articles