標籤:
1:什麼是HTTP?超文字傳輸通訊協定 (HTTP)(HyperText Transfer Protocol -- HTTP)是一個設計來使用戶端和伺服器順利進行通訊的協議。HTTP在用戶端和伺服器之間以request-response protocol(請求-回複協議)工作。2:HTTP編程
1>:使用get方式擷取伺服器端的一張圖片,放在本地==》擷取伺服器的東西用get方式更好
package get;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;public class HttpUtil{ /** * 使用get方式擷取伺服器端的一張圖片 * @throws IOException */ public static InputStream getInputStream(String path) throws IOException { URL url = new URL(path); HttpURLConnection con = (HttpURLConnection) url.openConnection(); //佈建要求方式 con.setRequestMethod("GET");//預設請求方式是get //設定連線逾時時間 con.setConnectTimeout(5000); //設定可以從伺服器端讀取資料 con.setDoInput(true);//預設值就是true InputStream in = null; //判斷伺服器端的響應碼是否是200 if(con.getResponseCode()==200) { in = con.getInputStream(); } return in; } public static void makeImg(InputStream in) throws IOException { FileOutputStream fos = new FileOutputStream("file\\img.jpg"); byte[] b = new byte[1024]; int len = 0; //讀取伺服器返回的圖片資料 while ((len = in.read(b))!=-1) { fos.write(b,0,len); } fos.close(); in.close(); }}物件導向代碼
package get;import java.io.IOException;public class Test{ public static void main(String[] args) throws IOException { String path ="http://localhost:9999/day16/img2.jpg"; HttpUtil.makeImg(HttpUtil.getInputStream(path)); }}用戶端
2:使用post方式提交使用者名稱和密碼,並擷取伺服器端的驗證結果==》需要返回資料就用post,伺服器端代碼上篇部落格中有
package post;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;public class HttpUtil{ public static String sendByPost(String path,Map<String,String> params,String encode) throws ClientProtocolException, IOException { //先把要提交的每對兒資料封裝成NameValuePair類型的 List<NameValuePair> list = new ArrayList<NameValuePair>(); for(Map.Entry<String, String> en:params.entrySet()) { list.add(new BasicNameValuePair(en.getKey(),en.getValue())); } //把要提交的資料群組織成提交的格式 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, encode); //建立提交方式對象 HttpPost post = new HttpPost(path); post.setEntity(entity); //建立執行提交的對象 HttpClient client = new DefaultHttpClient(); //執行提交 HttpResponse response = client.execute(post); if(response.getStatusLine().getStatusCode()==200) { InputStream in = response.getEntity().getContent(); return getResult(in,encode); } return null; } private static String getResult(InputStream in, String encode) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] arr = new byte[1024]; int len = 0; while((len = in.read(arr))!=-1) { bos.write(arr,0,len); } return new String(bos.toByteArray(),encode); }}物件導向代碼
package post;import java.io.IOException;import java.util.HashMap;import java.util.Map;public class Test{ /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { Map<String,String> params = new HashMap<String, String>(); params.put("username", "李鵬"); params.put("pwd","123456"); String path = "http://localhost:9999/day16/servlet/LoginServlet"; String encode= "utf-8"; System.out.println(HttpUtils.sendByPost(path, params, encode)); }}用戶端
3:使用apache中jar包提供的簡介方法提交使用者名稱和密碼,並擷取伺服器端的驗證結果
package apache;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;public class HttpUtil { public static String sendByPost(String path,HashMap<String,String> params,String encode) throws ClientProtocolException, IOException { //先把要提交的每對兒資料封裝成NameValuePair類型的 List<NameValuePair> list = new ArrayList<NameValuePair>(); for(Map.Entry<String, String> en:params.entrySet()) { list.add(new BasicNameValuePair(en.getKey(),en.getValue())); } //把要提交的資料群組織成提交的格式 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, encode); //建立提交方式對象 HttpPost post = new HttpPost(path); post.setEntity(entity); //建立執行提交的對象 HttpClient client = new DefaultHttpClient(); //執行提交 HttpResponse response = client.execute(post); if(response.getStatusLine().getStatusCode()==200) { InputStream in = response.getEntity().getContent(); return getResult(in,encode); } return null; } private static String getResult(InputStream in, String encode) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] arr = new byte[1024]; int len = 0; while((len = in.read(arr))!=-1) { bos.write(arr,0,len); } return new String(bos.toByteArray(),encode); }}物件導向代碼
package appache;import java.io.IOException;import java.util.HashMap;import java.util.Map;public class Test{ /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { Map<String,String> params = new HashMap<String, String>(); params.put("username", "李鵬"); params.put("pwd","123456"); String path = "http://localhost:9999/day16/servlet/LoginServlet"; String encode= "utf-8"; System.out.println(HttpUtil.sendByPost(path, params, encode)); }}用戶端代碼
java Http編程小結