日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 自动表单提交数据,Android 使用三种方式获取网页(通过Post,Get进行表单的提交)...

發布時間:2024/9/19 Android 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 自动表单提交数据,Android 使用三种方式获取网页(通过Post,Get进行表单的提交)... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

// 直接獲取信息

void DirectInfo() throws IOException {

URL url = new URL(SRC);

HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

InputStreamReader inStreamReader = new InputStreamReader(httpConn

.getInputStream());

BufferedReader bufReader = new BufferedReader(inStreamReader);

String line = "";

String Date = "OK";

while ((line = bufReader.readLine()) != null) {

Date += line + "\n";

}

edit1.setText(Date);

}

// get方式獲取信息

void getInfo() throws IOException {

// 將上面使用的方法直接修改一下即可。

URL url = new URL(SRC+"/default.aspx?NAME="

+ URLEncoder.encode("abc", "utf-8"));

HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();

InputStreamReader inputReader = new InputStreamReader(httpconn

.getInputStream());

BufferedReader bufReader = new BufferedReader(inputReader);

String line = "";

String Date = "";

while ((line = bufReader.readLine()) != null) {

Date += line;

}

edit1.setText(Date);

}

// Post方式獲取信息

void postInfo() throws MalformedURLException, IOException {

// Post 方法比Get方法需要設置的參數更多

HttpURLConnection httpconn = (HttpURLConnection) new URL(SRC)

.openConnection();

// post 方式,輸入輸出需要設置為true

httpconn.setDoInput(true);

httpconn.setDoOutput(true);

httpconn.setRequestMethod("POST"); // 設置為Post方式,默認為get方式

httpconn.setUseCaches(false); // 不使用緩存

httpconn.setInstanceFollowRedirects(true); // 重定向

httpconn.setRequestProperty("Content-type",

"Application/x-www-form-urlencoded"); // 設置連接 的Content-type類型為:

// application/x-www-form-urlencoded

httpconn.connect(); ? ? //連接

DataOutputStream out = new DataOutputStream(httpconn.getOutputStream()); ?//聲明數據寫入流

String NAME= "NAME="+URLEncoder.encode("fly_binbin", "UTF-8");

String PWD="PWD="+URLEncoder.encode("123456","UTF-8");

String content = NAME+"&"+PWD;

out.writeBytes(content);

out.flush();

out.close();

BufferedReader reader = new BufferedReader(new InputStreamReader(httpconn.getInputStream()));

String line = "";

String resultDate = "";

while((line=reader.readLine())!=null)

{

resultDate += line;

}

edit1.setText(resultDate);

}

// post方式第二種

private static boolean sendPostRequest(String path, Map params, String encoding) throws Exception{ // method=save&name=liming&timelength=78 StringBuilder sb = new StringBuilder(""); if(params!=null && !params.isEmpty()){ for(Map.Entry entry : params.entrySet()){ sb.append(entry.getKey()).append('=') .append(URLEncoder.encode(entry.getValue(), encoding)).append('&'); } sb.deleteCharAt(sb.length() - 1); } byte[] data = sb.toString().getBytes();//得到實體數據 URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("POST"); conn.setConnectTimeout(5*1000); conn.setDoOutput(true);//能過post方式提交數據,必須要允許輸出 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Content-Length", String.valueOf(data.length)); OutputStream outStream = conn.getOutputStream(); outStream.write(data); outStream.flush(); outStream.close(); if(conn.getResponseCode()==200){ return true; } return false; }

轉載自 ?http://www.cnblogs.com/fly_binbin/archive/2010/12/19/1910535.html

總結

以上是生活随笔為你收集整理的android 自动表单提交数据,Android 使用三种方式获取网页(通过Post,Get进行表单的提交)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。