Java URL and URLConnection
The URL class encapsulates the URL address into an object and provides a method to parse the URL address, such as obtaining the uri, host, and port.
URLConnection is a combination of URL objects and Socket connections, making it easier to obtain the connection Socket that initiates a URL request.1. URL
import java.net.MalformedURLException;import java.net.URL;public class URLDemo
a inputstream that is used to read from the connection. /* * Principle: * OpenConnection () * Returns a URLConnection object that represents the connection to the remote object referenced by the URL. * URLConnection conn = url.openconnection (); * SYSTEM.OUT.PRINTLN (connection); * The bottom of the package is the HTTP parsing method of the URL, this object is the Java built-in can resolve the specific pro
URLConnection and HttpURLConnection use classes in java.net, which belong to the standard Java interface.HttpURLConnection inherits from URLConnection, and the difference is only for HTTP connections with HttpURLConnection.Basic steps: 1) creating URLs and Urlconnection/httpurlconnection objects2) Setting the connection parameters3) connect to the server4) Write
The URL class encapsulates the URL address into an object, providing a way to resolve the URL address, such as getting the URI part, the host part, the port, and so on.URLConnection is a combination of a URL object and a socket connection, making it easier to get the connection socket that originated the URL request.1.URLImport Java.net.malformedurlexception;import Java.net.URL; Public class Urldemo{Public static void main(string[] args) throws Malformedurlexception {URL url =NewURL ("Http://1
JAVA notes: Java Network Programming (2) URL and URLConnection,
The full name of a URL is Uniform Resource Locator, which is a unified Resource Locator and can be used directly to find resources on the Internet.
URLConnection is a class that encapsulates methods used to access remote network resources. It can be used to establish connections with remote servers and check the attributes of remote resources.
JAVA 63rd-about Client Server amp; URL Class amp; URLConnection
Common clients and servers
Client:
Browser: IE: pop-up window, Cheetah: pop-up window, multiple tags, competitive effect
Server:
Server: TomCat: 1. process the request 2. Respond
To make TomCat work for us, the related classes written in java must implement interface Serverlet.
The powerful aspect of a browser is its parsing capability. It can be parsed in many formats.
Server and clien
URLs in the network (Uniform Resource Locator) are abbreviations for Uniform Resource locators. It represents the address of a resource on the Internet. Through the URL we can access various network resources on the Internet, such as the most common www,ftp site. URLs can be thought of as "pointers" to Internet resources, which can be used to obtain information about Internet resources, including information about the InputStream object acquiring the URL, and a connection
Get URL object,new out, construct parameter: path of StringCall the OpenConnection () method of the URL object to get the urlconnection ObjectCall the getInputStream () method of the urlconnection object to get the input stream InputStream ObjectRead output streamImportJava.io.InputStream;ImportJava.net.URL;Importjava.net.URLConnection; Public classUrldemo {/** * @paramargs*/ Public Static voidMain
Java Tour (34)--custom server, urlconnection, Regular expression feature, match, cut, replace, fetch, web crawler
We then say network programming, TCP
I. Customizing the service side
We directly write a server, let the local to connect, you can see what kind of effect
Packagecom. LGL. Socket;Import Java. IO. IOException;Import Java. IO. PrintWriter;Import Java. NET. ServerSocket;Import Java. NET. Socket;public class Browser
Abstract class URLConnection is a superclass of all classes that represents a communication link between an application and a URL. Instances of this class can be used to read and write resources referenced by this URL. In general, creating a connection to a URL requires several steps:OpenConnection () operates on parameters that affect the connection to a remote resource. Connect () interacts with the resource; Query header fields and content.
]; alert (ele.textcontent); } } } Script> Head> Body> inputtype= "button"value= "Call the WebService service using AJAX mode"onclick= "Sendajax ();"> Body>HTML>Requestbody request body, you can bring the plugin via MyEclipse, click Go, the following request body is the format we want.Two, using URLConnection method Packagecn.demo.client.urlconnection;ImportJava.io.InputStream;Importja
A normal downloadWhen a service uses a breakpoint to download, the response information is 206.Urlconnection-httpurlconnection. -Get URLConnection instances by URL.Normal Download ExamplePackage Cn.demo;import Java.io.fileoutputstream;import java.io.inputstream;import java.io.outputstream;import Java.math.bigdecimal;import Java.net.httpurlconnection;import Java.net.URL; Public classCommondown { Public Stati
save if (Number.equalsignorecase (Imagenumber)) {//Enter correct out.print ("Validate Pass");} else {//Auth code error Out.print ("Captcha Error");}//Regardless of the circumstances, the data stored by the program can only be used once Session.removeattribute ("Number");} else {out.print ("Verification Code Invalid");}}} webroot new login.html page 1.html= "keywords" content= "keyword1,keyword2,keyword3"> = "description" content= "This is my page"> = "Content-type" content= "text/html; Charset=
In Java, since the IP addresses are encapsulated in objects, the URL object must be encapsulatedImportJava.io.InputStream;ImportJava.net.URL;Importjava.net.URLConnection; Public classUrldemo { Public Static voidMain (string[] args)throwsException {//parse the data in the URL, using the URL objectString Str_url = "Http://192.168.17.1:8080/myapp/font.html?name=lisi"; URL URL=NewURL (str_url);//System.out.println ("Getprotocol:" +url.getprotocol ());//System.out.println ("GetHost:" +url.gethost ())
1. urlconnection
// 1. url
// 1> determine whether text starts with HTTP
Nsstring * urlstring = text;
If (! [Text hasprefix: @ "http: //"]) {
// 2> concatenate a URL string
Urlstring = [nsstring stringwithformat: @ "http://m.baidu.com/s? WORD = % @ ", text];
}
// % Escape is required for special characters, such as spaces or Chinese Characters
// In iOS, if there are no special requirements, the required encoding format is utf8
Urlstring =
The main difference between using urlconnection to call WebService and Ajax to call WebService is that the former uses Java to construct HTTP requests,
The latter uses the JS language to construct HTTP requests. The functions of these two methods are actually the same, but the implementation forms are different ····················
1. Server SideCodeYou can refer to using JDK to call WebService
2. The client code is as follows:
App
Package
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.