问题描述
使用socket连接到某一个网站,使用"GET/HTTP/1.0"获取html
解决方案
解决方案二:
/**Main.java**Createdon2008年7月30日,下午5:20**Tochangethistemplate,chooseTools|TemplateManager*andopenthetemplateintheeditor.*/packagehttp;importjava.io.*;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.net.HttpURLConnection;importjava.net.MalformedURLException;importjava.net.URL;importjava.net.URLConnection;importjava.net.URLEncoder;importjava.util.Map;importorg.omg.CORBA.Any;importorg.omg.DynamicAny.NameValuePair;/****@authorAdministrator*/publicclassMain{URLurl;/**CreatesanewinstanceofMain*/publicMain(){try{URLurl=newURL("http://127.0.0.1:8080/WebApplication1/action.jsp");HttpURLConnectionconnection=(HttpURLConnection)url.openConnection();connection.setRequestMethod("GET");System.out.println(connection.getRequestMethod());connection.setDoOutput(true);PrintWriterout=newPrintWriter(connection.getOutputStream());Stringname="name="+URLEncoder.encode("lixueling","UTF-8");Stringpass="pass="+URLEncoder.encode("jiayou","UTF-8");out.println(name+"&"+pass);out.close();BufferedReaderin=newBufferedReader(newInputStreamReader(connection.getInputStream()));Stringline;while((line=in.readLine())!=null){System.out.println(line);}in.close();}catch(Exceptionex){ex.printStackTrace();}}/***@paramargsthecommandlinearguments*/publicstaticvoidmain(String[]args){newMain();//TODOcodeapplicationlogichere}}line中的内容就是html内容了。
解决方案三:
顶一下
解决方案四:
学习拉!
解决方案五:
packageNet;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.net.HttpURLConnection;importjava.net.MalformedURLException;importjava.net.URL;importjava.net.URLConnection;publicclassTestURL{publicstaticvoidmain(String[]args){try{URLurl=newURL("http://www.sina.com.cn");//URLConnectionuc=url.openConnection();BufferedReaderbr=newBufferedReader(newInputStreamReader(url.openStream()));Strings=null;while((s=br.readLine())!=null){System.out.println(s);}br.close();}catch(MalformedURLExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}}
解决方案六:
学习了