Share URLConnection calling interface instance
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></jarr.length>
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!

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.

JavaisnotentirelyplatformindependentduetoJVMvariationsandnativecodeintegration,butitlargelyupholdsitsWORApromise.1)JavacompilestobytecoderunbytheJVM,allowingcross-platformexecution.2)However,eachplatformrequiresaspecificJVM,anddifferencesinJVMimpleme

TheJavaVirtualMachine(JVM)isanabstractcomputingmachinecrucialforJavaexecutionasitrunsJavabytecode,enablingthe"writeonce,runanywhere"capability.TheJVM'skeycomponentsinclude:1)ClassLoader,whichloads,links,andinitializesclasses;2)RuntimeDataAr

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

The five major features of Java are polymorphism, Lambda expressions, StreamsAPI, generics and exception handling. 1. Polymorphism allows objects of different classes to be used as objects of common base classes. 2. Lambda expressions make the code more concise, especially suitable for handling collections and streams. 3.StreamsAPI efficiently processes large data sets and supports declarative operations. 4. Generics provide type safety and reusability, and type errors are caught during compilation. 5. Exception handling helps handle errors elegantly and write reliable software.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)
