Http programming (1) using Java API to program Java API

Source: Internet
Author: User

Http programming (1) using Java API to program Java API

The common implementation method in Java Http programming is to use the APIS provided by Java, and the APIS provided by Apache.1Http programming through APIS provided by Java Class: URL: the class URL represents a unified resource locator, which is a pointer to the Internet "resource. HttpURLConnection: The URLConnection abstract class that supports specific HTTP functions is a super class of all classes. It represents the communication link between the application and the URL. This type of instance can be used to read and write resources referenced by this URL1.1. download data (taking downloading images as an example) import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java.net. httpURLConnection; import java.net. URL; public class DownloadImage {public static void main (String [] args) throws IOException {// resource URL: is a resource connection, the parameters in the URL can also be images on the Internet or connected to other resources // For example, change http: // localhost: 8080/Day_0818/aa.jpg to images You need to change the suffix URL = new url ("http: // localhost: 8080/Day_0818/aa.jpg") at the time of storage "); // obtain a url connection object that encapsulates the http protocol through a URL: HttpURLConnection connection = (HttpURLConnection) url. openConnection (); // set the connection request method. Because data is obtained, the request method is GET: the connection must be capitalized. setRequestMethod ("GET"); // sets whether to obtain the input stream of the connection. The default value is true. You can also skip this statement. setDoInput (true); // If a connection is established, the connection is enabled. connect (); // get the response code int code = connection. getRes PonseCode (); // if the response code is 200, the connection returns a successful response if (200 = code) {// obtain the connection input stream InputStream is = connection. getInputStream (); // file output stream object, (create a file to store resources) FileOutputStream fos = new FileOutputStream ("e: \ aa.jpg"); // byte array, I understand it as an intermediary between the input stream and the output stream. The input stream puts the data in an array to allow the output stream to read byte [] B = new byte [1024]; int length =-1; while (length = is. read (B ))! =-1) {fos. write (B, 0, length); fos. flush ();} // close the stream fos. close () ;}} login // post to simulate logon. /* Create a LoginServlet class to receive data */import java. io. inputStream; import java. io. outputStream; import java.net. httpURLConnection; import java.net. URL; // http: // localhost: 8080/MyServer/loginServlet? Username = admin & userpwd = 111 public class URLDemo2 {public static void main (String [] args) throws Exception {String path = "http: // localhost: 8080/MyServer/loginServlet "; URL url = new URL (path); HttpURLConnection connection = (HttpURLConnection) url. openConnection (); connection. setRequestMethod ("POST"); connection. setConnectTimeout (30000); connection. setDoInput (true); connection. setDoOutput (true );/ /Username = admin & userpwd = 111/** change the user name and password to the data entered by the user. */OutputStream OS = connection. getOutputStream (); OS. write ("username = admin & userpwd = 111 ". getBytes (); connection. connect (); int code = connection. getResponseCode (); if (code == 200) {InputStream is = connection. getInputStream (); byte [] B = new byte [1024]; int length = is. read (B); System. out. println (new String (B, 0, length); is. close ();}}}

 

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.