Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the URLConnection parameter in JDK

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What are the URLConnection parameters in JDK, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

In view of the problem of connecting URLConnection to Servlet in JDK, although it is involved on the Internet, it only shows that one or more problems are solved by FAQ, and they are relatively fragmented. Now, the use of this class is summarized as follows:

1: > categories of URL requests:

There are two categories, GET and POST requests. The difference between the two is:

A:) the get request can get the static page, or it can put the parameter after the URL string and pass it to servlet.

B:) post differs from get in that the parameters of post are not placed in the URL string, but in the body of the http request.

2: > object problem of URLConnection:

The object of URLConnection, the following code example:

/ / the following index.jsp is mapped to / / a Servlet (com.quantanetwork.getClientDataServlet) / / the attention point of the Servlet will mention URL url = new URL ("http://localhost:8080/TestHttpURLConnectionPro/index.jsp"); URLConnection rulConnection = url.openConnection ()). / / the urlConnection object here is actually a subclass HttpURLConnection of the URLConnection class / / generated according to URL's / / request protocol (in this case, http), so here * * convert it / / to an object of type HttpURLConnection in order to use / / HttpURLConnection more API. As follows: HttpURLConnection httpUrlConnection = (HttpURLConnection) rulConnection

3: > HttpURLConnection object parameter problem

/ / set whether to output to httpUrlConnection, because this is a post request, and the parameters should be placed in the body of / / http, so you need to set it to true, default to false; httpUrlConnection.setDoOutput (true); / / set whether to read from httpUrlConnection, default is true; httpUrlConnection.setDoInput (true); / / Post requests cannot use cache httpUrlConnection.setUseCaches (false) / / sets the content type transmitted to be a serializable java object / / (if this is not set, java.io.EOFException may be thrown when the WEB service defaults to a different type) httpUrlConnection.setRequestProperty ("Content-type", "application/x-java-serialized-object"); / / sets the request method to "POST", and defaults to GET httpUrlConnection.setRequestMethod ("POST") / / connection, the configuration from url.openConnection () to here in Article 2 above must be completed before connect, httpUrlConnection.connect ()

4: > HttpURLConnection connection problem:

/ / here getOutputStream implicitly carries out connect (that is, just like calling the above connect () method, / / so you don't have to call the above connect () in development). OutputStream outStrm = httpUrlConnection.getOutputStream ()

5: > HttpURLConnection write data and send data problems:

/ / now the object output stream object is built through the output stream object to output serializable objects. ObjectOutputStream objOutputStrm = new ObjectOutputStream (outStrm); / / writes data to the object output stream, which will be stored in the memory buffer objOutputStrm.writeObject (new String ("I am the test data"); / / refreshes the object output stream, writing any bytes to the potential stream (ObjectOutputStream in some places) objOutputStm.flush (); / / closes the stream object. At this point, no more data can be written to the object output stream, and the previously written data exists in the memory buffer. / the prepared http request is formally sent to the server objOutputStm.close () when the getInputStream () function below is called; / the getInputStream () function of the HttpURLConnection connection object is called, and / / the complete HTTP request message encapsulated in the memory buffer is sent to the server. InputStream inStrm = httpConn.getInputStream (); / / Development notes on the Servlet side:

A:) for HTTP requests of type POST sent by the client, Servlet must implement the doPost method, not the doGet method.

B:) use the getInputStream () method of HttpServletRequest to get the object of InputStream, such as:

InputStream inStream = httpRequest.getInputStream ()

Now when you call inStream.available () (this method is used to "return the estimated number of bytes that can be read (or skipped) from this input stream unblocked by the next method call to this input stream", it always returns 0. It is incorrect to attempt to allocate a buffer using the return value of this method to save all data from this stream. So, the solution now is to implement the Servlet side as follows:

InputStream inStream = httpRequest.getInputStream (); ObjectInputStream objInStream = new ObjectInputStream (inStream); Object obj = objInStream.readObject (); / / do subsequent processing / /. / / . . . . On the other hand, whether the client sends the actual data or not, it has to write to an object (even if this object is not used), such as: ObjectOutputStream objOutputStrm = new ObjectOutputStream (outStrm); objOutputStrm.writeObject (new String ("")); / / send an empty data here / / you can even send a null object, and the server will judge and process it after getting it. ObjOutputStrm.writeObject (null); objOutputStrm.flush (); objOutputStrm.close ()

Note: when creating an object output stream ObjectOutputStream, if the input stream obtained from HttpServletRequest (that is, outStrm in new ObjectOutputStream (outStrm)) is wrapped in the buffered OutputStream stream, there must be objOutputStrm.flush (); this sentence flushes the stream information into the buffered output stream. As follows:

ObjectOutputStream objOutputStrm = new ObjectOutputStream (new BufferedOutputStream (outStrm)); objOutputStrm.writeObject (null); objOutputStrm.flush (); / /

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report