One. Pros and cons of Get and post
1. Get
> Advantages: It is relatively simple to write, as long as the address behind the stitching data can be.
> Disadvantages: The submitted data has a length limit, the security is relatively poor
2.Post
> Advantages: The submitted data is not a length limit, the use of the flow of the way submitted, high security
> Cons: It's more difficult to write.
Two. Submit data using Post
1. Locating resources
String Path = "Http://10.0.2.2:8080/QQ/LoginServlet";
URL url = new URL (path);
2. Open the connection
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
3. How to set the request
1. How to set the request: Post
Conn.setrequestmethod ("POST"); Post capitalization
4. Set the data type of the submission
2. Set the data type of the submission
Conn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
5. Set the data length of the submission
String data = "qq=" +qq+ "&pwd=" +PWD;
3. Set the data length of the submission
Conn.setrequestproperty ("Content-length", data.length () + "");
6. Open the output stream
4. Open the output stream first
Conn.setdooutput (TRUE);
7. Start writing data to the server
5. Start writing data
OutputStream OS = Conn.getoutputstream ();
Os.write (Data.getbytes ());
Os.close ();
8. Get the data returned by the server (same as Get)
Three. Chinese garbled problem
1. Server returned data garbled
> When the server returns data, use UTF-8 to encode
> server uses the default Code table encoding, the client uses GBK's code table to decode
> Note: When the Tomcat server returns data, the default is to use the Local Code table to encode, if the Local Code table does not support this character, then use the Iso-8859-1 Code table to encode
2. When the client submits Chinese, garbled characters appear
2.1. When the client submits the URL encoding in Chinese
2.2. The server will decode
Since the Tomcat server obtains the data, the default is to use the Iso-8859-1 Code table to encode,But the client side is using UTF-8 's code table, so the string must be returned to its original state and then decoded
String newpwd = new String (Pwd.getbytes ("iso-8859-1"), "UTF-8");
Four. Submit data steps using HttpClient-post
1. Define the Client
1. Create a client
HttpClient client = new Defaulthttpclient ();
2. Defining the request
String Path = "Http://10.0.2.2:8080/QQ/LoginServlet";
2. Define a POST request
HttpPost request = new HttpPost (path);
3. Encapsulate the data entity for the request (GET method omit this step)
Define a collection of parameters on the band
list<namevaluepair> parameters = new arraylist<namevaluepair> ();
Put two parameters on the set, QQ-pwd
Parameters.Add (New Basicnamevaluepair ("QQ", QQ));
Parameters.Add (New Basicnamevaluepair ("pwd", pwd));
Define a data entity if you have Chinese, the encoding should be set at the time of the new entity object.
httpentity entity = new Urlencodedformentity (Parameters, "Utf-8");
3. Setting parameters for a POST request
Request.setentity (entity);
4. Execution of requests
3 Executing a GET request
HttpResponse res = client.execute (request);
5. Determine the status code according to the response object
4. Get the status code
int code = Res.getstatusline (). Getstatuscode ();
if (code ==200) {
。。。
}
6. Getting data content from a response object
5. Get the data entity inside the response object and get the input stream
InputStream is = Res.getentity (). getcontent ();
Data returned by the server
String result = Streamtools.readstream (IS);
Multi-Threaded Download
Network data access
> Thread concurrency
Why multi-threaded download speed is relatively fast
> Multiple threads Divide the server's resources at the same time, and everyone gets the content equally. Within a unit of time, the acquired resources become much more.
> The rate of multi-threaded downloads is also limited by the client's downstream bandwidth and the server's upstream bandwidth.
# # #多线程断点续传
1. When writing the data, save the current download amount.
2. When you enter, you must read the previous save location before starting the download, and then start the download from this location
3. The location of the jump should also be noted, not the previous startindex
Learn notes about get and post (v)