《java源碼使用URL來讀取網(wǎng)頁內(nèi)容》由會員分享,可在線閱讀,更多相關(guān)《java源碼使用URL來讀取網(wǎng)頁內(nèi)容(2頁珍藏版)》請在裝配圖網(wǎng)上搜索。
1、文檔供參考,可復(fù)制、編制,期待您的好評與關(guān)注!
package network;
import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import .HttpURLConnection;
import .URL;
import .URLEncoder;
public class GetDataByURL {
2、 public static String cc(String leibie, String num) {
StringBuffer temp = new StringBuffer();
try {
System.out.println(leibie);
System.out.println(num);
String url = "http://192.168.7.228:7890/oa/themes/mskin/login/login.jsp";
Htt
3、pURLConnection uc = (HttpURLConnection)new URL(url).
openConnection();
uc.setConnectTimeout(10000);
uc.setDoOutput(true);
uc.setRequestMethod("GET");
uc.setUseCaches(false);
DataOutputStream out = ne
4、w DataOutputStream(uc.getOutputStream());
// 瑕佷紶鐨勫弬鏁?/*
String s = URLEncoder.encode("ra", "GB2312") + "=" +
URLEncoder.encode(leibie, "GB2312");
s += "&" + URLEncoder.encode("keyword", "GB2312") + "=" +
URLEnco
5、der.encode(num, "GB2312");
*/
String s = URLEncoder.encode("ra", "UTF-8") + "=" +
URLEncoder.encode(leibie, "UTF-8");
s += "&" + URLEncoder.encode("keyword", "UTF-8") + "=" +
URLEncoder.encode(num, "UTF-8");
6、 // DataOutputStream.writeBytes灝嗗瓧絎︿覆涓殑16浣嶇殑unicode瀛楃浠?浣嶇殑瀛楃褰㈠紡鍐欓亾嫻侀噷闈? out.writeBytes(s);
out.flush();
out.close();
InputStream in = new BufferedInputStream(uc.getInputStream());
Reader rd = new InputStreamReader(in, "UTF-8");
7、 int c = 0;
while ((c = rd.read()) != -1) {
temp.append((char) c);
}
System.out.println(temp.toString());
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return temp.toString();
}
public static void main(String[] a){
GetDataByURL.cc("1","鍚塇");
}
}
2 / 2