1、做spider ,用C++还是Java,还是C#,哪个最快??
2. java的URL类和Socket 哪个更有优势,听说HTTP 1.1 可以保持连接,是不是对同一个Address 只用解析一次IP,就可以了? 以后只要复用这个Socket,附加一个请求文件的相当地址。
3. 用URL请求一个网站的多个网页,是不是每一次都要解析IP?

解决方案 »

  1.   

    1 你最擅长哪个,就用哪个
    2 Socket 可控制的更多一些,编码更复杂一些。 URL 则替你做了一些事情。强人就自己socket吧
    3 解析的结果你可以保存起来啊?至少你用socket方式,可以直接传递上次解析的IP地址,
      

  2.   

    时间并不耗在 DNS 解析上.HTTP 的一次数据交换(请求),必须要重建连接.....怎么样高效,只能在多线程上去想办法....
      

  3.   

    瑶蝶论坛近期将举行“瑶蝶建站暨第一届LOGO大赛”,比赛流程都以用公告写出,大家可以积极参加哦
      

  4.   

    建议用URL,HTTP协议是工作在应用层的,用Socket的话是直接处理IP层的报文,需要对IP协议有所了解才行
      

  5.   

    Socket的效率要高一些,URL更高层一些一定要多线程
      

  6.   

    回应2楼
    HTTP   的一次数据交换(请求),必须要重建连接..... 不是这样的,http 1.1 协议里面有keep-alive参数的,可以在一个连接里面请求多个页面。
      

  7.   

    回应5楼
    Socket的话是直接处理IP层的报文,需要对IP协议有所了解才行Socket不需要,也不能,控制IP协议,java已经替你封装了,我咱们只需要了解http协议即可,不需要了解底层的。
    而 HttpURLConnection 连http协议都替你封装了!
      

  8.   

    Http.java
    importjava.net.*;
    importjava.io.*;
    importjava.util.Properties;
    importjava.util.Enumeration;publicclassHttp{
    protectedSocketclient;
    protectedBufferedOutputStreamsender;
    protectedBufferedInputStreamreceiver;
    protectedByteArrayInputStreambyteStream;
    protectedURLtarget;
    privateintresponseCode=-1;
    privateStringresponseMessage="";
    privateStringserverVersion="";
    privatePropertiesheader=newProperties();
    publicHttp(){}
    publicHttp(Stringurl){
    GET(url);
    }/*GET方法根据URL,会请求文件、数据库查询结果、程序运行结果等多种内容*/
    publicvoidGET(Stringurl){
    try{
    checkHTTP(url);
    openServer(target.getHost(),target.getPort());
    Stringcmd="GET"+getURLFormat(target)+"HTTP/1.0\r\n"
    +getBaseHeads()+"\r\n";
    sendMessage(cmd);
    receiveMessage();
    }catch(ProtocolExceptionp){
    p.printStackTrace();
    return;
    }catch(UnknownHostExceptione){
    e.printStackTrace();
    return;
    }catch(IOExceptioni){
    i.printStackTrace();
    return;
    }
    }/*
    *HEAD方法只请求URL的元信息,不包括URL本身。若怀疑本机和服务器上的
    *文件相同,用这个方法检查最快捷有效。
    */
    publicvoidHEAD(Stringurl){
    try{
    checkHTTP(url);
    openServer(target.getHost(),target.getPort());
    Stringcmd="HEAD"+getURLFormat(target)+"HTTP/1.0\r\n"
    +getBaseHeads()+"\r\n";
    sendMessage(cmd);
    receiveMessage();
    }catch(ProtocolExceptionp){
    p.printStackTrace();
    return;
    }catch(UnknownHostExceptione){
    e.printStackTrace();
    return;
    }catch(IOExceptioni){
    i.printStackTrace();
    return;
    }
    }
    /*
    *POST方法是向服务器传送数据,以便服务器做出相应的处理。例如网页上常用的
    *提交表格。
    */
    publicvoidPOST(Stringurl,Stringcontent){
    try{
    checkHTTP(url);
    openServer(target.getHost(),target.getPort());
    Stringcmd="POST"+getURLFormat(target)+"HTTP/1.0\r\n"+getBaseHeads();
    cmd+="Content-type:application/x-www-form-urlencoded\r\n";
    cmd+="Content-length:"+content.length()+"\r\n\r\n";
    cmd+=content+"\r\n";
    sendMessage(cmd);
    receiveMessage();
    }catch(ProtocolExceptionp){
    p.printStackTrace();
    return;
    }catch(UnknownHostExceptione){
    e.printStackTrace();
    return;
    }catch(IOExceptioni){
    i.printStackTrace();
    return;
    }
    }protectedvoidcheckHTTP(Stringurl)throwsProtocolException{
    try{
    URLtarget=newURL(url);
    if(target==null||!target.getProtocol().toUpperCase().equals("HTTP"))
    thrownewProtocolException("这不是HTTP协议");
    this.target=target;
    }catch(MalformedURLExceptionm){
    thrownewProtocolException("协议格式错误");
    }
    }/*
    *与Web服务器连接。若找不到Web服务器,InetAddress会引发UnknownHostException
    *异常。若Socket连接失败,会引发IOException异常。
    */
    protectedvoidopenServer(Stringhost,intport)throws
    UnknownHostException,IOException{
    header.clear();
    responseMessage="";responseCode=-1;
    try{
    if(client!=null)closeServer();
    if(byteStream!=null){
    byteStream.close();byteStream=null;
    }
    InetAddressaddress=InetAddress.getByName(host);
    client=newSocket(address,port==-1?80:port);
    sender=newBufferedOutputStream(client.getOutputStream());
    receiver=newBufferedInputStream(client.getInputStream());
    }catch(UnknownHostExceptionu){
    throwu;
    }catch(IOExceptioni){
    throwi;
    }
    }/*关闭与Web服务器的连接*/
    protectedvoidcloseServer()throwsIOException{
    if(client==null)return;
    try{
    client.close();sender.close();receiver.close();
    }catch(IOExceptioni){
    throwi;
    }
    client=null;sender=null;receiver=null;
    }protectedStringgetURLFormat(URLtarget){
    Stringspec="http://"+target.getHost();
    if(target.getPort()!=-1)
    spec+=":"+target.getPort();
    returnspec+=target.getFile();
    }/*向Web服务器传送数据*/
    protectedvoidsendMessage(Stringdata)throwsIOException{
    sender.write(data.getBytes(),0,data.length());
    sender.flush();
    }/*接收来自Web服务器的数据*/
    protectedvoidreceiveMessage()throwsIOException{
    bytedata[]=newbyte[1024];
    intcount=0;
    intword=-1;
    //解析第一行
    while((word=receiver.read())!=-1){
    if(word=='\r'||word=='\n'){
    word=receiver.read();
    if(word=='\n')word=receiver.read();
    break;
    }
    if(count==data.length)data=addCapacity(data);
    data[count++]=(byte)word;
    }
    Stringmessage=newString(data,0,count);
    int=message.indexOf(32);
    serverVersion=message.substring(0,);
    while(<message.length()&&message.charAt(+1)==32)++;
    responseCode=Integer.parseInt(message.substring(+1,+=4));
    responseMessage=message.substring(,message.length()).trim();//应答状态码和处理请读者添加
    switch(responseCode){
    case400:
    thrownewIOException("错误请求");
    case404:
    thrownewFileNotFoundException(getURLFormat(target));
    case503:
    thrownewIOException("服务器不可用");
    }
    if(word==-1)thrownewProtocolException("信息接收异常终止");
    intsymbol=-1;
    count=0;
    //解析元信息
    while(word!='\r'&&word!='\n'&&word>-1){
    if(word=='\t')word=32;
    if(count==data.length)data=addCapacity(data);
    data[count++]=(byte)word;
    parseLine:{
    while((symbol=receiver.read())>-1){
    switch(symbol){
    case'\t':
    symbol=32;break;
    case'\r':
    case'\n':
    word=receiver.read();
    if(symbol=='\r'&&word=='\n'){
    word=receiver.read();
    if(word=='\r')word=receiver.read();
    }
    if(word=='\r'||word=='\n'||word>32)breakparseLine;
    symbol=32;break;
    }
    if(count==data.length)data=addCapacity(data);
    data[count++]=(byte)symbol;
    }
    word=-1;
    }
    message=newString(data,0,count);
    =message.indexOf(':');
    Stringkey=null;
    if(>0)key=message.substring(0,);
    ++;
    while(<message.length()&&message.charAt()<=32)++;
    Stringvalue=message.substring(,message.length());
    header.put(key,value);
    count=0;
    }
    //获得正文数据
    while((word=receiver.read())!=-1){
    if(count==data.length)data=addCapacity(data);
    data[count++]=(byte)word;
    }
    if(count>0)byteStream=newByteArrayInputStream(data,0,count);
    data=null;
    closeServer();
    }publicStringgetResponseMessage(){
    returnresponseMessage;
    }publicintgetResponseCode(){
    returnresponseCode;
    }publicStringgetServerVersion(){
    returnserverVersion;
    }publicInputStreamgetInputStream(){
    returnbyteStream;
    }publicsynchronizedStringgetHeaderKey(inti){
    if(i>=header.size())returnnull;
    Enumerationenum=header.propertyNames();
    Stringkey=null;
    for(intj=0;j<=i;j++)
    key=(String)enum.nextElement();
    returnkey;
    }publicsynchronizedStringgetHeaderValue(inti){
    if(i>=header.size())returnnull;
    returnheader.getProperty(getHeaderKey(i));
    }publicsynchronizedStringgetHeaderValue(Stringkey){
    returnheader.getProperty(key);
    }protectedStringgetBaseHeads(){
    Stringinf="User-Agent:myselfHttp/1.0\r\n"+
    "Accept:www/source;text/html;image/gif;*/*\r\n";
    returninf;
    }privatebyte[]addCapacity(byterece[]){
    bytetemp[]=newbyte[rece.length+1024];
    System.arraycopy(rece,0,temp,0,rece.length);
    returntemp;
    }
    publicstaticvoidmain(String[]args){
    Httphttp=newHttp();
    //http.GET("http://192.168.1.5");
    inti;
    for(i=0;i<50000;i++){
    http.GET("http://www.model-dl.com/modelinfo.asp?modelid=101");
    http.POST("http://www.model-dl.com/modelinfo.asp?modelid=101","ratecontd=101&MM_insert=form1");
    }
    }}
      

  9.   

    1、做spider   ,用C++还是Java,还是C#,哪个最快??到底哪个速度最快??
    只是从速度上去客观的评论!
      

  10.   

    显然 spider 的性能不是关键...
    重要的是带宽,以及 爬取的任务管理..如果一个站,爬得过猛.只会被封掉.spider 我的建议是使用 相对灵活些的脚本去做,更好.
    ep: python, perl
      

  11.   

    显然用URL啊, Socket是处理TCP协议的, 处理HTTP协议的话, 你得读多少RFC啊?