java 使用HttpURLConnection發(fā)送數(shù)據(jù)簡(jiǎn)單實(shí)例
每個(gè) HttpURLConnection 實(shí)例都可用于生成單個(gè)請(qǐng)求,但是其他實(shí)例可以透明地共享連接到 HTTP 服務(wù)器的基礎(chǔ)網(wǎng)絡(luò)。請(qǐng)求后在 HttpURLConnection 的 InputStream 或 OutputStream 上調(diào)用 close() 方法可以釋放與此實(shí)例關(guān)聯(lián)的網(wǎng)絡(luò)資源,但對(duì)共享的持久連接沒(méi)有任何影響。如果在調(diào)用 disconnect() 時(shí)持久連接空閑,則可能關(guān)閉基礎(chǔ)套接字。JAVA使用HttpURLConnection發(fā)送POST數(shù)據(jù)是依靠OutputStream流的形式發(fā)送
實(shí)現(xiàn)代碼:
import java.io.*; import java.net.*; public class PostExample { public static void main(String[] argv) throws Exception { URL url = new URL("http://www.javacourses.com/cgi-bin/names.cgi"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); PrintWriter out = new PrintWriter(connection.getOutputStream()); // encode the message String name = "name="+URLEncoder.encode("Qusay Mahmoud", "UTF-8"); String email = "email="+URLEncoder.encode("qmahmoud@javacourses.com", "UTF-8"); // send the encoded message out.println(name+"&"+email); out.close(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { System.out.println(line); } in.close(); } }
分享題目:java使用HttpURLConnection發(fā)送數(shù)據(jù)簡(jiǎn)單實(shí)例-創(chuàng)新互聯(lián)
當(dāng)前鏈接:http://www.chinadenli.net/article8/doicop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、標(biāo)簽優(yōu)化、App設(shè)計(jì)、定制網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容
網(wǎng)頁(yè)設(shè)計(jì)公司知識(shí)