In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to achieve Java Socket response". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to achieve Java Socket response.
HTTP/1.1 indicates that the HTTP server is version 1.1, which is the server's response status code to the customer's request, and OK is the interpretation of the response status code, followed by the document's meta-information and document body. (for an explanation of response status codes and meta-information, please see the draft Inetrnet standard: RFC2616).
Http.java import java.net.*; import java.io.*; import java.util.Properties; import java.util.Enumeration; public class Http {protected Socket client; protected BufferedOutputStream sender; protected BufferedInputStream receiver; protected ByteArrayInputStream byteStream; protected URL target; private int responseCode=-1; private String responseMessage= "; private String serverVersion="; private Properties header = new Properties (); public Http () {} public Http (String url) {GET (url) } / * GET method requests files, database query results, program running results and other contents according to URL * / public void GET (String url) {try {checkHTTP (url); openServer (target.getHost (), target.getPort ()); String cmd = "GET" + getURLFormat (target) + "HTTP/1.0\ r\ n" + getBaseHeads () + "\ r\ n"; sendMessage (cmd); receiveMessage () } catch (ProtocolException p) {p.printStackTrace (); return;} catch (UnknownHostException e) {e.printStackTrace (); return;} catch (IOException I) {i.printStackTrace (); return;}} / * * HEAD method requests only the meta-information of URL, excluding URL itself. If you suspect that the * files on this machine are the same as those on the server, this method is the quickest and most effective way to check. * / public void HEAD (String url) {try {checkHTTP (url); openServer (target.getHost (), target.getPort ()); String cmd = "HEAD" + getURLFormat (target) + "HTTP/1.0\ r\ n" + getBaseHeads () + "\ r\ n"; sendMessage (cmd); receiveMessage ();} catch (ProtocolException p) {p.printStackTrace (); return;} catch (UnknownHostException e) {e.printStackTrace (); return The} catch (IOException I) {i.printStackTrace (); return;}} / * * POST method sends data to the server so that the server can process it accordingly. For example, the * submission form commonly used on the web page. * / public void POST (String url,String content) {try {checkHTTP (url); openServer (target.getHost (), target.getPort ()); String cmd = "POST" + getURLFormat (target) + "HTTP/1.0\ r\ n" + getBaseHeads (); cmd + = "Content-type: application/x-www-form-urlencoded\ r\ n"; cmd + = "Content-length:" + content.length () + "\ r\ n\ n"; cmd + = content+ "\ r\ n" SendMessage (cmd); receiveMessage ();} catch (ProtocolException p) {p.printStackTrace (); return;} catch (UnknownHostException e) {e.printStackTrace (); return;} catch (IOException I) {i.printStackTrace (); return;}} protected void checkHTTP (String url) throws ProtocolException {try {URL target = new URL (url) If (target==null |! target.getProtocol (). ToUpperCase (). Equals ("HTTP") throw new ProtocolException ("this is not the HTTP protocol"); this.target = target;} catch (MalformedURLException m) {throw new ProtocolException ("protocol format error");}} / * * connect to the Web server. If the Web server cannot be found, InetAddress throws a UnknownHostException * exception. If the Socket connection fails, an IOException exception is thrown. * / protected void openServer (String host,int port) throws UnknownHostException,IOException {header.clear (); responseMessage= ""; responseCode=-1; try {if (clientless null) closeServer (); if (byteStream! = null) {byteStream.close (); byteStream=null;} InetAddress address = InetAddress.getByName (host); client = new Socket (address,port==-1?80:port); sender = new BufferedOutputStream (client.getOutputStream ()); receiver = new BufferedInputStream (client.getInputStream ()) } catch (UnknownHostException u) {throw u;} catch (IOException I) {throw I;}} / * close the connection to the Web server * / protected void closeServer () throws IOException {if (client==null) return; try {client.close (); sender.close (); receiver.close ();} catch (IOException I) {throw I;} client=null; sender=null; receiver=null } protected String getURLFormat (URL target) {String spec = "http:// + target.getHost (); if (target.getPort ()! =-1) spec+=": "+ target.getPort (); return spec+=target.getFile ();} / * transfer data to the Web server * / protected void sendMessage (String data) throws IOException {sender.write (data.getBytes (), 0protected void sendMessage data.length ()); sender.flush () } / * receive data from Web server * / protected void receiveMessage () throws IOException {byte data [] = new byte [1024]; int count=0; int word=-1; / / parse * line while ((word=receiver.read ())! =-1) {if (word=='\ r'| | word=='\ n') {word=receiver.read (); if (word=='\ n') word=receiver.read (); break } if (count==data.length) data = addCapacity (data); data [count++] = (byte) word;} String message = new String (data,0,count); int mark = message.indexOf (32); serverVersion = message.substring (0score mark); while (mark-1) {if (word=='\ t') word=32; if (count==data.length) data = addCapacity (data); data [count++] = (byte) word ParseLine: {while ((symbol=receiver.read ()) >-1) {switch (symbol) {case'\ tasking: symbol=32; break; case'\ rsectors: case'\ nregions: word=receiver.read (); if (symbol=='\ r' & & word=='\ n') {word=receiver.read (); if (word=='\ r') word=receiver.read ();} if (word=='\ r' | word=='\ n'| | word > 32) break parseLine; symbol=32 Break;} if (count==data.length) data = addCapacity (data); data [count++] = (byte) symbol;} word=-1;} message = new String (data,0,count); mark= message.indexOf (':'); String key = null; if (mark > 0) key = message.substring (0line mark); mark++; while (mark=header.size () return null; Enumeration enum = header.propertyNames (); String key = null; for (int jig0) J=header.size () return null; return header.getProperty (getHeaderKey (I));} public synchronized String getHeaderValue (String key) {return header.getProperty (key);} protected String getBaseHeads () {String inf = "User-Agent: myselfHttp/1.0\ r\ n" + "Accept: www/source; text/html; image/gif; * / *\ r\ n"; return inf } private byte [] addCapacity (byte rece []) {byte temp [] = new byte [rece.length+1024]; System.arraycopy (rece,0,temp,0,rece.length); return temp;} public static void main (String [] args) {Http http=new Http (); / / http.GET ("http://192.168.1.5); int i; for (iTuno; I)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.