第一种:以流的形式读取服务器传过来的结果 pr otect ed st ring GetNe wMail Title (stri ngho st,intpo rt,s tring acco unt, strin gdom ainNa me) { stri ngne wMail Title = st ring. Empty ;s tring newM ailTe xt="未能获取到您的新邮件。"; try { stri ngqu erySt ring = "qu ery?a ct=ne wtitl e&uid ="+ accou nt+ "&dom ain=" +do mainN ame + ""; IPAd dress ip= IPAd dress .Pars e(hos t); IPEnd Point ipe = new IPEn dPoin t(ip, port ); S ocket myso cket = new Sock et(Ad dress Famil work, Sock etTyp eam, Proto colTy p); mysoc onnec t(ipe ); N etwor kStre am ns= workS tream (myso cket) ; St reamW riter sw = new Strea mWrit er(ns ); E ncodi ngg bk=En codin Encod ing(" GBK") ; St reamR eader sr= new Strea mRead er(ns ,gbk) ; st ring cmdPr ompt = sr. ReadL ine() ;if (cm dProm uals( "helo ")) { UTF 8Enco ding encod er= new U TF8En codin g(); byte [] se ndQue ryTem p=e ncode Bytes (quer yStri ng); byte[ ] sen dQuer y=n ew by te[se ndQue ryTem gth + 2]; char[ ] clo seCha r=n ew ch ar[2] ; clo seCha r[0] = (ch ar)(1 0); c loseC har[1 ]=( char) (13); byte [] cl oseBy te= Encod etByt es(cl oseCh ar); for (int i=0;i< send Query Temp. Lengt h; i+ +) {s endQu ery[i ]=s endQu eryTe mp[i] ; } sendQ uery[ sendQ uery. Lengt h-2]=c loseB yte[0 ]; se ndQue ry[se ndQue ngth -1]= clo seByt e[1]; myso cket. Send( sendQ uery, 0,s endQu ength , Soc ketFl one); //En codin
c#socket中文出现乱码编码转换 来自淘豆网m.daumloan.com转载请标明出处.