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

How to analyze URLConnection parameters in JDK

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

Share

Shulou(Shulou.com)05/31 Report--

This article is to share with you about how to analyze URLConnection parameters in JDK. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

URLConnection parameters in JDK

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 points of the Servlet will be mentioned below.

URL url = new URL ("http://localhost:8080/TestHttpURLConnectionPro/index.jsp");

URLConnection rulConnection = url.openConnection (); / / the urlConnection object here is actually based on the URL

/ / request the URLConnection class generated by the protocol (in this case, http)

The subclass HttpURLConnection of / /, so it is best to convert it here

/ / is an object of type HttpURLConnection, so that you can use the

/ / HttpURLConnection more API. It is 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

/ / within the http body, so you need to set it to true. By default, it is false.

HttpUrlConnection.setDoOutput (true)

/ / sets whether to read from httpUrlConnection. Default is true.

HttpUrlConnection.setDoInput (true)

/ / caching cannot be used for Post requests

HttpUrlConnection.setUseCaches (false)

/ / set the content type to be serializable java object

/ / (if this is not set, java.io.EOFException may be thrown when a serialized object is delivered when the WEB service defaults to a different type)

HttpUrlConnection.setRequestProperty ("Content-type", "application/x-java-serialized-object")

/ / set the request method to "POST". The default is GET.

HttpUrlConnection.setRequestMethod ("POST")

/ / connection, the configuration of url.openConnection () from Article 2 above must be completed before connect

HttpUrlConnection.connect ()

4: > HttpURLConnection connection problem:

/ / here getOutputStream implicitly connect (that is, just like calling the connect () method above

/ / so it's okay not 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)

/ / write data to the object output stream, which will be stored in the memory buffer

ObjOutputStrm.writeObject (new String ("I am test data"))

/ / refresh the object output stream and write any bytes to the potential stream (ObjectOutputStream in some places)

ObjOutputStm.flush ()

/ / close 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 officially sent to the server only when the following getInputStream () function is called.

ObjOutputStm.close ()

/ / call the getInputStream () function of the HttpURLConnection connection object

/ / send the complete HTTP request message encapsulated in the memory buffer 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 call inStream.available () (this method is used to "return the next method call to this input stream can be unblocked

When the estimated number of bytes is read (or skipped) from this input stream, 0 is always returned. Try to allocate a buffer using the return value of this method

It is incorrect to save all data in this stream. So, the solution now is

The Servlet end is implemented as follows:

InputStream inStream = httpRequest.getInputStream ()

ObjectInputStream objInStream = new ObjectInputStream (inStream)

Object obj = objInStream.readObject ()

/ / do follow-up processing

/ / .

/ / . . . .

On the other hand, whether the client sends the actual data or not, it has to write to an object (even if the 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 can get it and then make a judgment.

ObjOutputStrm.writeObject (null)

ObjOutputStrm.flush ()

ObjOutputStrm.close ()

Note: when creating the object output stream ObjectOutputStream above, if the input stream obtained from HttpServletRequest

(that is, outStrm in new ObjectOutputStream (outStrm)) is wrapped in BufferedOutputStream

Then there must be objOutputStrm.flush (); this sentence to brush the stream information into the buffered output stream. It is 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

Servers

Wechat

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

12
Report