JavaWeb涔婼ervlet&缃戠粶璁块棶鏈嶅姟鍣ㄧ紪绋�

1聽 聽 聽 Servlet&缃戠粶璁块棶

1.1聽聽聽聽缃戣矾鍩虹

B/S鐨凷聽 Server聽http缃戠粶搴旂敤鏈嶅姟绔蒋浠�

http聽Hyper Text Transmission Protocol 瓒呮枃鏈紶杈撳崗璁紝澶勪簬鍥涘眰鏋舵瀯涓殑搴旂敤灞傦紝鏄缓绔婽CP鐨勫熀纭�涓�

缃戠粶搴旂敤鏈嶅姟绔蒋浠跺彲浠ョ湅鎴愪竴涓鍣紝鎴戜滑灏嗙綉缁滃簲鐢ㄩ儴缃插埌鍏朵腑骞跺惎鍔紝杩欎釜瀹瑰櫒灏卞彲浠ユ寜鐓ф垜浠殑鎰忔効鍘诲伐浣滐紝澶勭悊瀹㈡埛绔殑璇锋眰骞跺搷搴�

甯哥敤缃戠粶搴旂敤鏈嶅姟绔蒋浠�:Tomcat銆丣Boss銆乄ebsphere銆乄ebLogic

濡備綍鍐橶eb椤圭洰:

1銆丠ttpServlet 灏忕殑鏈嶅姟绔粍浠� 瀹氫箟涓�涓被缁ф壙瀹�

2銆侀噸鍐欏叾涓殑doGet銆乨oPost鏂规硶,鍦ㄨ繖涓や釜鏂规硶閲屽啓涓婂浣曞鐞嗗鎴风鐨勮姹傦紝杩欎袱涓柟娉曟槸鍥炶皟鏂规硶锛宒oGet澶勭悊Get璇锋眰銆乨oPost澶勭悊Post璇锋眰

3銆佸湪web.xml涓敞鍐岃繖涓被锛屽憡璇夊鍣ㄥ湪澶勭悊鏌愪竴绫诲瀷璇锋眰鐨勬椂鍊欙紝浣跨敤杩欎釜绫讳腑鐨勯�昏緫

鍙互浣跨敤HttpServletRequest鐨刧etParameter鏂规硶鑾峰彇璇锋眰涓煇鍙傛暟鐨勫��

鍙互浣跨敤HttpServletResponse鐨刧etOutputStream鏂规硶鑾峰彇鍝嶅簲鐨勮緭鍑烘祦锛岀粰瀹㈡埛绔啓淇℃伅

C/S 涓殑 C

URL绫宦� Uniform Resource Locator缁熶竴璧勬簮瀹氫綅鍣�

鏋勯�犳柟娉曪細URL(String url)鎶婁竴涓瓧绗︿覆褰㈠紡鐨剈rl鏋勫缓鍑轰竴涓猆RL瀵硅薄

openConnection() 鑾峰彇缃戠粶杩炴帴URLConnection瀵硅薄锛屼竴鑸細灏嗗叾寮鸿浆涓篐ttpURLConnection绫诲瀷锛屽苟浣滅浉搴旀搷浣溿��(璋冪敤杩欎釜鏂规硶鏃惰繖涓繛鎺ュ苟娌℃湁鐪熸浜х敓)

HttpURLConnection 绫�

涓昏鏂规硶:

connect()聽浜х敓鐪熸鐨勮繛鎺�

getInputStream() 鑾峰彇鍝嶅簲浣撹緭鍏ユ祦锛屽彲浠ョ敤鏉ユ帴鏀舵湇鍔$鍝嶅簲

getResponseCode()鑾峰彇鍝嶅簲鐘舵�佺爜锛�200涓烘甯革紝杩欎釜鏂规硶蹇呴』鍦ㄨ繛鎺ヤ箣鍚庝互鍙婅姹傚彂鍑哄幓涔嬪悗鎵嶈皟鐢紝鍚﹀垯瀹冧細闅愬紡璋冪敤connect()鏂规硶锛屽嚭鐜颁竴浜涘鎬殑鐘跺喌

getOutputStream()鑾峰彇璇锋眰浣撶殑杈撳嚭娴侊紝鍙互寰�璇锋眰浣撲腑鍐�

setRequestMethod()璁剧疆璇锋眰鏂瑰紡锛岄粯璁や负GET锛屽鏋滄槸POST鏂瑰紡灏变竴瀹氳浣跨敤杩欎釜鏂规硶

setDoOutput() 璁剧疆鏄惁鍙互寰�杩炴帴涓緭鍑猴紝榛樿鏄痜alse锛屽鏋滄槸POST鏂瑰紡锛屼竴瀹氳璁句负true

setDoInput() 璁剧疆鏄惁鍙粠杩炴帴涓鍑猴紝榛樿涓簍rue,鎵�浠ヨ繖涓柟娉曚笉闇�瑕佷娇鐢�

GET璇锋眰:

鎶婂弬鏁版斁鍦╱rl閲岄潰锛屽拰鏈嶅姟绔繛鎺ワ紝璇诲彇鏈嶅姟绔搷搴�

1銆佸垱寤篣RL(鍚庨潰璺熺潃鍙傛暟 )

2銆佽幏鍙栬繛鎺�

3銆佷骇鐢熻繛鎺�

4銆佸垽鏂姸鎬佺爜锛屾槸200灏辫鍙栧搷搴斿唴瀹�

POST璇锋眰:

1銆佸垱寤篣RL(鍚庨潰娌″弬鏁�)

2銆佽幏鍙栬繛鎺�

3銆佽缃姹傛柟娉晄etRequestMethod("POST")

璁剧疆鍏佽杈撳嚭setDoOutput(true),鍏佽寰�璇锋眰(浣�)閲屽啓

4銆佷骇鐢熻繛鎺�

5銆佽幏鍙栬繛鎺ョ殑杈撳嚭娴侊紝寰�璇锋眰(浣�)涓啓鍙傛暟

6銆佽鍙栧搷搴斿唴瀹�

1.2聽聽聽聽Web姒傝堪

1.2.1聽 缃戠粶鏋舵瀯锛欳/S 涓� B/S涔嬩簤

銆�

C/S锛欳lient/Server锛氥�婁紭鐐癸細瀹㈡埛绔彲浠ヤ换鎰忚璁★紝鍙互灞曠ず缁氫附鐨勬晥鏋滃拰鐗规畩鍔熻兘锛涘浜庣綉閫熺殑渚濊禆娌℃湁B/S閭d箞寮�

缂虹偣锛氫娇鐢ㄦ椂闇�瑕佷笅杞藉鎴风骞跺畨瑁咃紝鎵�鏈夊鎴风閮介渶瑕佽仈缃戞洿鏂般��

B/S锛欱rowser/Server锛氥�婁紭鐐癸細涓嶉渶瑕佷笅杞戒换浣曞鎴风锛屽彧瑕佹湁娴忚鍣ㄥ氨鍙互浣跨敤锛屽綋绋嬪簭闇�瑕佹洿鏂版椂锛屽彧闇�瑕佸湪鏈嶅姟绔繘琛屾洿鏂板嵆鍙�

缂虹偣锛氭墍鏈夌殑杩愮畻閮借鍦ㄦ湇鍔$瀹屾垚锛屾湇鍔″櫒鍘嬪姏鍜屽ぇ锛屽苟涓旀祻瑙堝櫒鏆傛椂涓嶅叿澶囧睍绀哄崄鍒嗙粴涓界殑鏁堟灉鐨勮兘鍔涳紱鍗佸垎渚濊禆缃戠粶锛堢綉閫燂級銆�

Http鍗忚锛氥�奌ypertext Transfer Protocol锛氳秴鏂囨湰浼犺緭鍗忚锛屽畠鏄互TCP/IP涓哄熀纭�鐨勯珮灞傚崗璁�

瀹氫箟娴忚鍣ㄥ拰web鏈嶅姟鍣ㄤ箣闂翠氦鎹㈡暟鎹殑杩囩▼浠ュ強鏁版嵁鏍煎紡

瀹氫箟Web娴忚鍣ㄥ拰web鏈嶅姟鍣ㄤ箣闂撮�氳鐨勫崗璁�

鐜板湪骞挎硾浣跨敤鐨勭増鏈槸Http/1.1銆傜浉瀵逛簬Http/1.0鏉ヨ锛屾渶澶х殑鐗圭偣灏辨槸鏀寔鎸佺画杩炴帴锛堝嵆涓�娆CP杩炴帴涓婂彲浠ュ寘鍚娆¤姹傚拰鍝嶅簲锛�

甯歌鐘舵�佺爜锛氥�婃秷鎭紙1瀛楀ご锛�:100聽聽聽聽 鎴愬姛锛�2瀛楀ご锛夛細200聽聽 閲嶅畾鍚戯紙3瀛楀ご锛�

璇锋眰閿欒锛�4瀛楀ご锛夛細404聽聽聽 鏈嶅姟鍣ㄩ敊璇紙5銆�6瀛楀ご锛夛細500銆�

銆�

銆�

1.3聽聽聽聽Servlet鐨勫熀鏈蹇�

銆�

Servlet鏄疭UN鍏徃鎻愪緵鐨勪竴绉嶇敤浜庡紑鍙戝姩鎬亀eb璧勬簮鐨勬妧鏈�

Sun鍏徃鍦ㄥ叾API涓彁渚涗簡涓�涓猄ervlet鎺ュ彛锛屾牴鎹鎺ュ彛缂栧啓鐨勭▼搴忔病鏈塵ain鏂规硶銆傚畠浠彈鎺т簬鍙︿竴涓�Java搴旂敤锛岃繖涓狫ava搴旂敤绉颁负Servlet瀹瑰櫒锛孴omcat灏辨槸杩欐牱涓�涓鍣�

銆�

1.3.1聽 Servlet鐨勭櫥闄嗙ず渚�

銆�

Inserttitle here

-->

action:鏈嶅姟鍣ㄧ殑鍦板潃

method:鏁版嵁鐨勬彁浜ゆ柟寮�,甯哥敤鐨勬湁涓ょ锛歡et銆乸ost

-->

鐢ㄦ埛鍚嶏細

瀵嗙爜锛�

publicclass MyServlet extends HttpServlet {

// 搴忓垪鍊�

private static final longserialVersionUID = 1L;

/**

* @see HttpServlet#HttpServlet()

*/

public MyServlet() {

super();

// TODO Auto-generated constructor stub

}

/**

*

* 鐢ㄤ簬澶勭悊鏉ヨ嚜瀹㈡埛绔殑缃戠粶璇锋眰锛坓et鏂瑰紡锛�

* 鍙傛暟1锛歳equest锛屾潵鑷鎴风鐨勮姹傚璞★紝灏佽浜嗗鎴风鍚戞湇鍔$鍙戦�佺殑璇锋眰鏁版嵁

* 鍙傛暟2锛歳esponse锛岄�氳繃璇ュ璞″彲浠ュ悜瀹㈡埛绔仛鍑哄搷搴�

*/

protected void doGet(HttpServletRequestrequest, HttpServletResponse response) throws ServletException, IOException {

// 鑾峰彇璐﹀彿銆佸瘑鐮�

response.setCharacterEncoding("GBK");

String name= request.getParameter("username");

String pwd= request.getParameter("pwd");

System.out.println(name+ "..." + pwd);

if("admin".equals(name)&& "123".equals(pwd)) {

//鐧婚檰鎴愬姛

response.getWriter().print("loginsuccess...锛堢櫥闄嗘垚鍔燂級");

} else {

//鐧婚檰澶辫触

response.getWriter().print("loginfailed...锛堢櫥闄嗗け璐ワ級");

}

}

/**

* 鐢ㄤ簬澶勭悊鏉ヨ嚜瀹㈡埛绔殑缃戠粶璇锋眰锛坧ost鏂瑰紡锛�

*/

protected voiddoPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

// TODO Auto-generated methodstub

this.doGet(request,response);

}

}

銆�

鍩轰簬JSP鐨勫疄鐜板舰寮忥細

銆�

<%@page language="java" import="java.util.*"pageEncoding="utf-8"%>

<%

Stringpath = request.getContextPath();

%>

娴嬭瘯HTTP鍗忚浣撶殑鍐呭

-->

-->

/servlet/LoginAction">

鐢ㄦ埛鍚嶏細


瀵喡� 鐮侊細


publicclass LoginAction extends HttpServlet {

/**

* Constructor of the object.

*/

public LoginAction() {

super();

}

/**

* Destruction of the servlet.

*/

public void destroy() {

super.destroy(); // Just puts"destroy" string in log

// Put your code here

}

public void doGet(HttpServletRequestrequest, HttpServletResponse response)

throwsServletException, IOException {

this.doPost(request,response);

}

public void doPost(HttpServletRequestrequest, HttpServletResponse response)

throwsServletException, IOException {

response.setContentType("text/html;charset=utf-8");

request.setCharacterEncoding("utf-8");

response.setCharacterEncoding("utf-8");

PrintWriter out =response.getWriter();

String username =request.getParameter("username");

System.out.println("-username->>"+username);

String pswd =request.getParameter("password");

System.out.println("-password->>"+pswd);

if(username.equals("admin")&&pswd.equals("123")){

//琛ㄧず鏈嶅姟鍣ㄧ杩斿洖鐨勭粨鏋�

out.print("loginis success!!!!");

}else{

out.print("loginis fail!!!");

}

out.flush();

out.close();

}

public void init() throwsServletException {

// Put your code here

}

}

銆�

1.4聽聽聽聽缃戠粶璁块棶

1.4.1聽 URL

銆�

浠�涔堟槸URL锛燂細銆奤RL锛圲niformResource Locator锛夋槸缁熶竴璧勬簮瀹氫綅绗︾殑绠�绉�

琛ㄧずIntent涓婃煇涓�璧勬簮鐨勫湴鍧�

閫氳繃URL鍙互璁块棶Intenet涓婄殑鍚勭缃戠粶璧勬簮锛屾瘮濡傚父瑙佺殑www銆丗TP绔欑偣銆�

URL鐨勫熀鏈粨鏋勭敱5閮ㄥ垎缁勬垚锛氥��<浼犺緭鍗忚>://<涓绘満鍚�>:<绔彛鍙�>/<鏂囦欢鍚�>#<寮曠敤>

http://www.tomcat.com:80/Gamelan/network.html#BOTTOM銆�

銆�

1.4.2聽 鑾峰彇缃戠粶鏁版嵁

銆�

鏂瑰紡涓�锛欻ttpUrlConnection绫伙細銆�1銆佽幏鍙栫綉缁滆繛鎺ュ璞�

1銆� 璁剧疆缃戠粶鏁版嵁璇锋眰鐨勬柟寮忥紙榛樿GET锛�

2銆� 璁剧疆杩炴帴瓒呮椂鐨勬椂闂达紝濡傛灉瓒呮椂锛岃鏄庢病鏈夎繛鎺ユ垚鍔燂紝灏变細鎶涘嚭寮傚父

3銆� 璁剧疆鎺ユ敹鏁版嵁瓒呮椂鐨勬椂闂达紝濡傛灉瓒呮椂锛屽氨浼氭帓闄ゅ紓甯�

4銆� 楠岃瘉鏈嶅姟鍣ㄧ殑杩斿洖鐮�

5銆� 閫氳繃IO娴佷紶杈撴暟鎹�

6銆� 瑙f瀽鏁版嵁

PS锛歅OST璇锋眰闇�瑕佸彟澶栬缃姹傚弬鏁�

Content-Type:application/x-www-form-urlencoded

Content-Length: 鏁版嵁鐨勫ぇ灏�

銆�

銆�

鏂瑰紡浜岋細ApacheHttpClient妗嗘灦锛氥��

銆奌ttpClient鏄敱Apache寮�婧愮粍缁囨彁渚涚殑涓�涓紑婧愰」鐩紝瀹冩槸涓�涓畝鍗曠殑Http瀹㈡埛绔紙骞朵笉鏄祻瑙堝櫒锛夛紝鍙互鐢ㄤ簬鍙戦�丠ttp璇锋眰锛屾帴鏀禜ttp鍝嶅簲銆�

銆婄畝鍗曟潵璇达紝HttpClient灏辨槸涓�涓寮虹増鐨凥ttpURLConnection锛孒ttpURLConnection鍙互鍋氱殑浜嬫儏HttpClient鍏ㄩ儴鍙互鍋氥�侶ttpURLConnection娌℃湁鎻愪緵鐨勬湁浜涘姛鑳斤紝HttpClient涔熸彁渚涗簡锛屼絾瀹冨彧鏄叧娉ㄤ簬濡備綍鍙戦�佽姹傘�佹帴鏀跺搷搴旓紝浠ュ強绠$悊Http鐨勮繛鎺ャ��

銆�Android宸茬粡鎴愬姛闆嗘垚浜咹ttpClient锛欰ndroid6.0寮�濮嬶紝宸茬粡灏咹ttClient妗嗘灦浠嶢ndroidSDK涓Щ闄ゃ��

銆�

1.4.3聽 Apache HttpClient妗嗘灦

銆婃敞鎰忥細闇�瑕佸姞鍏ュ閮ㄧ殑Java鍖呫��

銆�

銆�

publicclass Demo {

public static void main(String[] args){

// TODO Auto-generated methodstub

//聽聽聽聽聽聽聽聽聽聽聽聽聽聽 doGetByHttpClient();

doPostByHttpClient();

}

private static voiddoPostByHttpClient() {

// TODO Auto-generated methodstub

HttpClient client = null;

try {

client = newDefaultHttpClient();

HttpPost post = newHttpPost("http://api.k780.com:88/");

//淇濆瓨璇锋眰鍙傛暟鐨勯泦鍚堝璞�

Listlist = new ArrayList<>();

//灏嗚姹傚弬鏁板皝瑁呮垚瀵硅薄瀛樺叆闆嗗悎涓紝姣忎竴涓狽ameValuePair瀵硅薄灏佽浜嗕竴缁勯敭鍊煎

list.add(newBasicNameValuePair("app", "weather.today"));

list.add(new BasicNameValuePair("weaid","1"));

list.add(newBasicNameValuePair("appkey", "15250"));

list.add(newBasicNameValuePair("sign","f88a5cecc3cbd37129bc090c0ae29943"));

list.add(newBasicNameValuePair("format", "json"));

HttpEntity httpEntity = newUrlEncodedFormEntity(list);

post.setEntity(httpEntity);

HttpResponse response =client.execute(post);

int code =response.getStatusLine().getStatusCode();

if(code == 200){

InputStream is =response.getEntity().getContent();

String result =readStream(is);

System.out.println(result);

}

} catch(UnsupportedEncodingException e) {

// TODOAuto-generated catch block

e.printStackTrace();

} catch(IllegalStateException e) {

// TODOAuto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODOAuto-generated catch block

e.printStackTrace();

}finally{

if(client != null){

client.getConnectionManager().shutdown();

client =null;

}

}

}

private static void doGetByHttpClient(){

// TODO Auto-generated methodstub

HttpClient client = null;

try {

client聽 = new DefaultHttpClient();

HttpGet get = newHttpGet("http://api.k780.com:88/?app=weather.today&weaid=1&&appkey=15250&sign=f88a5cecc3cbd37129bc090c0ae29943&format=json");

//璁块棶缃戠粶璧勬簮

HttpResponseresponse = client.execute(get);

//鑾峰彇鐘舵�佺爜

int code=response.getStatusLine().getStatusCode();

if(code == 200){

InputStreamis = response.getEntity().getContent();

Stringresult = readStream(is);

System.out.println(result);

}else{

throw newRuntimeException("缃戠粶璁块棶澶辫触锛�"+code);

}

} catch(ClientProtocolException e) {

// TODOAuto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODOAuto-generated catch block

e.printStackTrace();

}finally{

if(client !=null){

client.getConnectionManager().shutdown();

client =null;

}

}

}

public static StringreadStream(InputStream is) throws IOException{

ByteArrayOutputStream baos =new ByteArrayOutputStream();

byte[] buf = new byte[1024];

int len = 0;

while((len=is.read(buf))!=-1){

baos.write(buf, 0,len);

}

return newString(baos.toByteArray(), "utf-8");

}

}

銆�

1.5聽聽聽聽K780鏁版嵁缃�

銆�

K780鏁版嵁缃戯紝娴嬭瘯璐﹀彿

銆夾ppkey锛�15250聽聽聽聽 Secret锛�2bbebb3e480a850df6daca0c04a954e1

Sign锛歠88a5cecc3cbd37129bc090c0ae29943銆�

銆�

1.6聽聽聽聽绀轰緥浣跨敤Post鍜孏et璇锋眰缃戠粶鏁版嵁

銆�

publicclass NetPostGet {

/**

* @param args

*/

public static void main(String[] args){

// TODO Auto-generated methodstub

// doGet();

doPost();

}

// 浣跨敤post鏂瑰紡璇锋眰缃戠粶鏁版嵁

private static void doPost() {

HttpURLConnection conn =null;

try {

URL url = newURL("http://api.k780.com:88/");

conn =(HttpURLConnection) url.openConnection();

String data ="app=weather.today&weaid=1&&appkey=15250&sign=f88a5cecc3cbd37129bc090c0ae29943&format=json";

conn.setRequestMethod("POST");

conn.setReadTimeout(5000);

conn.setRequestProperty("Content-Type",

"application/x-www-form-urlencoded");

conn.setRequestProperty("Content-Length",data.length() + "");

// post缃戠粶璁块棶蹇呴』瀹炵幇

conn.setDoInput(true);

conn.setDoOutput(true);

// 娉ㄦ剰瑕佸皢璇锋眰鐨勫唴瀹瑰啓鍒拌姹備綋涓幓

conn.getOutputStream().write(data.getBytes());

conn.connect();

int code =conn.getResponseCode();

if (code == 200) {//琛ㄧず璁块棶鎴愬姛

InputStreamis = conn.getInputStream();

System.out.println(readStream(is));

} else {

System.out.println("缃戠粶鏁版嵁璁块棶澶辫触:" + code);

}

} catch(MalformedURLException e) {

// TODOAuto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODOAuto-generated catch block

e.printStackTrace();

} finally {

if (conn != null) {

conn.disconnect();

conn =null;

}

}

}

// 浣跨敤get鏂瑰紡璇锋眰缃戠粶鏁版嵁

private static void doGet() {

HttpURLConnection conn =null;

try {

URL url = new URL(

"http://api.k780.com:88/?app=weather.today&weaid=1&&appkey=15250&sign=f88a5cecc3cbd37129bc090c0ae29943&format=json");

conn =(HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");

conn.setReadTimeout(5000);

conn.setReadTimeout(5000);

conn.connect();

int code =conn.getResponseCode();

if (code == 200) {

InputStreamis = conn.getInputStream();

System.out.println(readStream(is));

} else {

System.out.println("鑾峰彇鏁版嵁澶辫触:" + code);

}

} catch(MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (conn != null) {

conn.disconnect();

conn =null;

}

}

}

public static StringreadStream(InputStream is) throws IOException {

ByteArrayOutputStream baos =new ByteArrayOutputStream();

int len = 0;

byte[] buf = new byte[1024];

while ((len = is.read(buf))!= -1) {

baos.write(buf, 0,len);

}

return newString(baos.toByteArray(), "utf-8");

}

}

銆�

1.7聽聽聽聽缃戠粶璁块棶宸ュ叿绫诲皝瑁�

銆�

publicclass HttpUtils {

private static final int TIMEOUT =10000;

//杩斿洖涓�涓瓧鑺傛暟缁�

public static byte[] doGet(Stringpath){

try {

URL mUrl = newURL(path);

HttpURLConnectionconn = (HttpURLConnection)mUrl.openConnection();

conn.setRequestMethod("GET");

conn.setReadTimeout(TIMEOUT);

conn.setReadTimeout(TIMEOUT);

conn.connect();

int code =conn.getResponseCode();

if(code == 200){

return readStream(conn.getInputStream());

}else{

throw newRuntimeException("缃戠粶璁块棶澶辫触锛�"+code);

}

} catch(MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

public static byte[] doPost(String url,String params) {

HttpURLConnection conn =null;

try {

URL mUrl = newURL(url);

conn =(HttpURLConnection) mUrl.openConnection();

conn.setRequestMethod("POST");

conn.setConnectTimeout(TIMEOUT);

conn.setReadTimeout(TIMEOUT);

// 璁剧疆璇锋眰灞炴��

conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

conn.setRequestProperty("Content-Length",params.length()+"");

// Post璇锋眰蹇呴』瑕佸啓浠ヤ笅涓よ浠g爜

conn.setDoInput(true);

conn.setDoOutput(true);

// 灏嗚姹傚弬鏁板啓鍒拌姹備綋涓�

conn.getOutputStream().write(params.getBytes());;

conn.connect();

int code =conn.getResponseCode();

if(code == 200) {

returnreadStream(conn.getInputStream());

} else {

throw newRuntimeException("缃戠粶璁块棶澶辫触锛�"+ code);

}

} catch (Exception e) {

e.printStackTrace();

return null;

} finally {

if(conn != null) {

conn.disconnect();

conn =null;

}

}

}

//杩斿洖涓�涓瓧绗︿覆

public static String doGetStr(Stringpath) throws IOException{

byte[] data = doGet(path);

return new String(data, "utf-8");

}

private static byte[]readStream(InputStream is) throws IOException{

ByteArrayOutputStream baos =new ByteArrayOutputStream();

byte[] buf = new byte[1024];

int len =0;

while((len =is.read(buf))!=-1){

baos.write(buf, 0,len);

}

return baos.toByteArray();

}

}

銆�

1.8聽聽聽聽鎬荤粨

銆�

鍦ˋndroid涓缃戠粶璇锋眰鏈�鍩烘湰鐨勫疄鐜版湁涓ょ

1銆伮� HttpURLConnection

鏄疛DK鑷甫鐨勫疄鐜版柟寮忥紝瀹炵幇鏂瑰紡鐩稿姣旇緝绻佺悙锛屼篃姣旇緝鐏垫椿

2銆伮� HttpClient妗嗘灦

搴曞眰浠g爜杩樻槸鍩轰簬HttpURLConnection,鍙槸瀵笻ttpURLConnection杩涜浜嗘洿娣卞眰娆$殑灏佽

鏄痑pache寮�婧愮粍缁囧紑鍙戠殑寮�婧愭鏋讹紝瀹炵幇鏂瑰紡鐩稿绠�鍗曚竴浜涳紝鐩稿HttpURLConnection鏉ヨ锛屾病鏈夐偅涔堢伒娲�

Android鍦⊿DK(SoftwareDevelopment Kit)涓凡缁忛泦鎴愪簡HttpClient妗嗘灦

涓嶈繃锛孉ndroid鍦�6.0寮�濮嬶紝灏卞凡缁忓皢HttpClient妗嗘灦浠嶴DK涓Щ闄や簡

銆�

你可能感兴趣的:(JavaWeb涔婼ervlet&缃戠粶璁块棶鏈嶅姟鍣ㄧ紪绋�)