问题描述
http://bill.finance.sina.com.cn/bill/detail.php?stock_code=sh600000&bill_size=40000这个页面,用了什么编码都是乱码,真是郁闷啊
解决方案
解决方案二:
privatevoidbtnTest_Click(objectsender,EventArgse){HttpWebRequestreq=(HttpWebRequest)WebRequest.Create("http://bill.finance.sina.com.cn/bill/detail.php?stock_code=sh600000&bill_size=40000");req.Method="GET";HttpWebResponsersp=(HttpWebResponse)req.GetResponse();StreamReaderrdr=newStreamReader(rsp.GetResponseStream(),Encoding.GetEncoding("GB2312"));strings=rdr.ReadToEnd();MessageBox.Show(s.Substring(0,1024));}
取出来的结果是------------------------------------------------------<html><head><metahttp-equiv="Content-type"content="text/html;charset=gb2312"><metahttp-equiv="pragma"content="no-cache"><metahttp-equiv="refresh"content="180"><metaname="keywords"content="大单追踪"><metaname="description"content="大单追踪"><title>600000_大单明细_大单追踪_新浪财经</title><styletype="text/css">td,p,li,select,input,textarea{font-size:12px}AIMG{border:0}.img01{border:1pxsolid#999999}.f14{font-size:14px}.f12{font-size:12px}.l15{line-height:150%}.l13{line-height:130%}.lh19{line-height:19px;}A:link{color:#0015EA}A:hover{color:#ff0000;}A.sinatail:link,A.sinatail:visited{text-decoration:underline;color:#0000ff;font-size:12px}A.sinatail:active,A.sinatail:hover{text-decoration:underline;color:#ff0000;font-size:12px}A.title1:link,A.title1:visited,A.title1:active,A.title1:hover{FONT-WEIGHT:bold;FONT-SIZE:17px;COLOR:#000000;FONT-FAMILY:arial;TEXT-DECORATION:none;}A.title2:link,A.title2:visited{COLOR:#000000;TEXT-DECORATION:none;}A.title2:active,A.title2:hover{C---------------------------确定---------------------------
解决方案三:
不行啊,照抄你的代码出来的是輢麠]Wq嫦璇竔Vh{蒉颎K瓕,蓪&€Y栿`f鸵狠}諁璙wэ晞痧??a虬別Y朵噇K贫0z8苿?d?......还是乱码!!!
解决方案四:
publicstaticstringCatchData(stringurl){WebClient_client=newWebClient();_client.BaseAddress=url;_client.Headers.Add("Accept","image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,application/x-shockwave-flash,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,*/*");_client.Headers.Add("Accept-Language","zh-cn");_client.Headers.Add("UA-CPU","x86");_client.Headers.Add("Accept-Encoding","gzip,deflate");_client.Headers.Add("User-Agent","Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.2;SV1;.NETCLR1.1.4322;.NETCLR2.0.50727)");System.IO.StreamobjStream=_client.OpenRead("/");System.IO.StreamReader_read=newSystem.IO.StreamReader(objStream,System.Text.Encoding.GetEncoding("GB2312"));return_read.ReadToEnd();}用WebRequest来实现的话是乱码用这个方法来实现的话出来的就是这结果,不是我想要的<html>n<head>n<title>股票行情_财经纵横_新浪网</title>n<metahttp-equiv="Content-type"content="text/html;charset=gb2312">n<scriptlanguage="javascript">n<!--nfunctiononGoDetailClick()n{ntvarcountry=document.getElementById("bill_country_id").value;ntvarstock_code_value=document.getElementById("stock_code_id").value;ntif(stock_code_value.length!=6)nt{nttalert("请输入正确的6位股票代码,如600000");nt}ntelsent{nttvarurl="http://bill.finance.sina.com.cn/bill/detail.php?stock_code="+country+stock_code_value;nttwindow.open(url,"","");nt}n}nnfunctiononGoTradeItemClick()n{ntvarcountry=document.getElementById("bill_country_id").value;ntvarstock_code_value=document.getElementById("stock_code_id").value;ntif(stock_code_value.length!=6)nt{nttalert("请输入正确的6位股票代码,如600000");nt}ntelsent{nttvarurl="http://bill.finance.sina.com.cn/bill/trade_item.php?stock_code="+country+stock_code_value;nttwindow.open(url,"","");nt}n}nnfunctiononGoPriceStatistClick()n{ntvarcountry=document.getElementById("bill_country_id").value;ntvarstock_code_value=document.getElementById("stock_code_id").value;ntif(stock_code_value.length!=6)nt{nttalert("请输入正确的6位股票代码,如600000");nt}ntelsent{nttvarurl="http://bill.finance.sina.com.cn/bill/price_statist.php?stock_code="+country+stock_code_value;nttwindow.open(url,"","");nt}n}nnfunctiononGoHoldStatistClick()n{ntvarcountry=document.getElementById("bill_country_id").value;ntvarstock_code_value=document.getElementById("stock_code_id").value;ntif(stock_code_value.length!=6)nt{nttalert("请输入正确的6位股票代码,如600000");nt}ntelsent{nttvarurl="http://bill.finance.sina.com.cn/bill/hold_statist.php?stock_code="+country+stock_code_value;nttwindow.open(url,"","");nt}n}n//-->n</script>n</head>n<body>n<center>nn<br/>nn<h1>n<ahref='http://bill.finance.sina.com.cn/bill/all.php'target='_blank'>实时大单</a><ahref='http://bill.finance.sina.com.cn/bill/analyse.php'target='_blank'>大单分析</a><ahref='http://bill.finance.sina.com.cn/bill/strange_bill.php'target='_blank'>异动大单</a><ahref='http://bill.finance.sina.com.cn/bill/history_all.php'target='_blank'>历史大单</a>n</h1>nn交易所:<selectid='bill_country_id'>n<optionvalue='sh'>上海</option>n<optionvalue='sz'selected>深圳</option>n</select> 股票代码:<inputtype='text'id='stock_code_id'size='6'value='000001'onclick='this.value=""'>n<inputtype='button'value='大单明细'onclick='onGoDetailClick()'>n<inputtype='button'value='成交明细'onclick='onGoTradeItemClick()'>n<inputtype='button'value='分价表'onclick='onGoPriceStatistClick()'>n<inputtype='button'value='持仓分析'onclick='onGoHoldStatistClick()'>nn<!--suggestbegin-->n<linkrel="stylesheet"href="http://finance.sina.com.cn/iframe/sg.css">n<scripttype="text/javascript">nfunctioniask(fn){ntif(fn.q.value==""||fn.q.value=="代码/名称/拼音")nt{nttfn.q.value="代码/名称/拼音";nttfn.q.focus();nttreturnfalse;nt}ntvarlocadr=fn.locadr.value;ntif(locadr!="")nt{nttwindow.open(locadr);nt}ntelsent{nttfn.symbol.value=fn.q.value;nttfn.submit();nt}ntreturnfalse;n}nfunctiong(URL){window.location.href=URL;}n</script>n<scriptlanguage="text/javascript">nfunctionString.prototype.xor(str)n{nvarresult="",l=Math.min(str.length,this.length);nfor(vari=0;i<l;i++)result+=String.fromCharCode(this.charCodeAt(i)^str.charCodeAt(i));nreturnresult;n}nvarstr1="K6/1&=1Z^AOCYS@77;$57]";nvarstr2="welcomezouyccrtaveagoodtimecc";n</script>n<scriptlanguage=javascript>nvarrecnum;nvarreclist=newArray;n</script>n<formmethod="post"id="f"name=iask_fonSubmit="returniask(this)"action="http://biz.finance.sina.com.cn/suggest/lookup.php"target=_blank>nt<inputtype="text"id="k"name="q"value="代码/名称/拼音"size="35"maxlength="50"autocomplete="off"onClick="javascript:if(this.value=='代码/名称/拼音')this.value='';">nt<inputtype="submit"value="查询"/>nt<inputname="s"type="hidden"value="1">nt<inputname="portnum"id="portnum"type="hidden"value="8081">nt<inputtype="hidden"name="symbol"id="symbol">nt<inputtype="hidden"name="market"id="market">nt<inputtype="hidden"name="locadr"id="locadr">n<scriptsrc="http://finance.sina.com.cn/iframe/za.js"></script>n<scriptlanguage=javascriptid="recscript"></script>n<scriptlanguage=javascript>neverytenms();n</script>n</form>n<!--suggestend-->nn</center>n</body>n</html>nn
解决方案五:
没人知道吗?有的时候乱码,有的时候又不乱。。难道我人品有时有问题,有时又没问题吗?!
解决方案六:
哈哈,一定是的
解决方案七:
哦,差点忘记了……你注意检查一下返回的HTTP头,看ContentType是不是gzip压缩的如果是压缩的,你需要解压
解决方案八:
给你一段代码……恩,只是片段,无法运行的privatevoid_NavigateTo(objectvURL){_IsBusy=true;_URL=(string)vURL;HttpWebRequestReq=(HttpWebRequest)WebRequest.Create(_URL);Req.AllowAutoRedirect=true;Req.KeepAlive=true;Req.Method="GET";Req.Accept="*";Req.Referer=_Referer;Req.UserAgent="Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1;+eglic.com)";Req.Headers[HttpRequestHeader.Cookie]=Cookie;HttpWebResponseRsp;try{Rsp=(HttpWebResponse)Req.GetResponse();}catch(Exceptione){_IsBusy=false;if(null!=OnError)OnError(this,4,e.Message);return;}_Headers=null;_Headers=Rsp.Headers.ToByteArray();//设置新的CookieCookie=Rsp.Headers[HttpResponseHeader.SetCookie];if(HttpStatusCode.NotFound==Rsp.StatusCode){_IsBusy=false;if(null!=OnError)OnError(this,2,"未找到网页");return;}elseif(HttpStatusCode.InternalServerError==Rsp.StatusCode){_IsBusy=false;if(null!=OnError)OnError(this,3,"服务器内部错误");return;}if(Rsp.Headers[HttpResponseHeader.ContentEncoding]=="gzip"){GZipStreamstmz=newGZipStream(Rsp.GetResponseStream(),CompressionMode.Decompress);MemoryStreamstmm=newMemoryStream();byte[]_zbuff=newbyte[2048];intread=-1;read=stmz.Read(_zbuff,0,2048);while(read>0){stmm.Write(_zbuff,0,read);read=stmz.Read(_zbuff,0,2048);}stmz.Close();stmz.Dispose();_HTML=Encoding.GetEncoding(_Lang).GetString(stmm.ToArray());stmm.Close();stmm.Dispose();}else{//设置编码StreamReaderstm=newStreamReader(Rsp.GetResponseStream(),Encoding.GetEncoding(_Lang));_HTML=stm.ReadToEnd();stm.Dispose();}ParseHTML();_Referer=_URL;_IsBusy=false;if(null!=OnDocumentReady)OnDocumentReady(this,false);}