问题描述
publicclassApiServicer{privateHashtabledllhash=newHashtable();publicHashtableDllHash{get{returndllhash;}}privatestring_webaddress=null;publicstaticexternintGetSystemMetrics(intnIndex);publicintGetScreenWidth(){intnWidth=GetSystemMetrics((int)enumScreen.SM_CXSCREEN);returnnWidth;}publicintGetScreenHeight(){intnHeight=GetSystemMetrics((int)enumScreen.SM_CYSCREEN);returnnHeight;}publicstringWebAddress{get{try{if(_webaddress==null){StreamReadersr=newStreamReader(ApplicationPath()+"\cfg.ini");StringRet="";Stringtmp="";while((tmp=sr.ReadLine())!=null){Ret+=tmp;}sr.Close();_webaddress=Ret;}}catch(Exceptionex){};return_webaddress;}set{_webaddress=value;}}publicstringApplicationPath(){stringappPath=System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);returnappPath;}publicApiServicer(){}publicApiServicer(stringurl){_webaddress=url;}publicstringGetParams(stringcmd,object[]Params){stringNewParams="<contextfunction=""+cmd+"">";NewParams+="<p>"+(userinfo==null?"":userinfo.userkey)+"</p>";if(Params==null){}else{for(inti=0;i<Params.Length;i++){if(cmd=="PdaQuery")NewParams+="<p>"+Params[i].ToString().Replace(">",">").Replace("<","<")+"</p>";elseNewParams+="<p>"+Params[i].ToString()+"</p>";}}NewParams+="</context>";returnNewParams;}///<summary>//////</summary>///<paramname="command"></param>///<paramname="Params"></param>///<returns></returns>publicobjectRunCmd(stringcommand,object[]Params){EncodingmyEncoding=Encoding.GetEncoding("UTF-8");byte[]postBytes=Encoding.ASCII.GetBytes(GetParams(command,Params));HttpWebRequestreq=(HttpWebRequest)WebRequest.Create(WebAddress);req.Method="POST";req.ContentType="text/html;charset=UTF-8";req.ContentLength=postBytes.Length;try{using(StreamreqStream=req.GetRequestStream()){reqStream.Write(postBytes,0,postBytes.Length);}//HttpWebResponsewr=(HttpWebResponse)req.GetResponse();using(WebResponsewr=req.GetResponse()){//OpenthestreamusingaStreamReaderforeasyaccess.StreamReaderreader=newStreamReader(wr.GetResponseStream());//Readthecontent.stringresponseFromServer=reader.ReadToEnd();returnDeCompress(responseFromServer);//解压缩数据//在这里对接收到的页面内容进行处理}}catch(Exceptionex){MessageBox.Show(ex.Message.ToString(),"提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button1);returnnull;}}publicbyte[]StreamToBytes(Streamstream){byte[]bytes=newbyte[stream.Length];stream.Seek(0,SeekOrigin.Begin);stream.Read(bytes,0,bytes.Length);//设置当前流的位置为流的开始returnbytes;}///<summary>///将byte[]转成Stream///</summary>publicStreamBytesToStream(byte[]bytes){Streamstream=newMemoryStream(bytes);returnstream;}publicbyte[]StringToBytes(stringtemp){returnConvert.FromBase64String(temp);}publicobjectDeCompress(stringtemp){DataTabledt=DeserializeDataTable(temp);if(dt!=null&&dt.Rows.Count>0){if((int)dt.Rows[0]["Type"]==0)//字符串直接返回return(string)dt.Rows[0]["Data"];elseif((int)dt.Rows[0]["Type"]==1){returnDeserializeDataTable(dt.Rows[0]["Data"].ToString());}}returnnull;}//publicobjectDbDeCompress(byte[]temp)//{//byte[]da=(byte[])temp;//MemoryStreaminput=newMemoryStream();//input.Write(da,0,da.Length);//input.Position=0;//GZipStreamgzip=newGZipStream(input,CompressionMode.Decompress,true);//MemoryStreamoutput=newMemoryStream();//byte[]buff=newbyte[4096];//intread=-1;//read=gzip.Read(buff,0,buff.Length);//while(read>0)//{//output.Write(buff,0,read);//read=gzip.Read(buff,0,buff.Length);//}//gzip.Close();//byte[]rebytes=output.ToArray();//output.Close();//input.Close();//MemoryStreamms=newMemoryStream(rebytes);//BinaryFormatterbf=newBinaryFormatter();//objectobj=bf.Deserialize(ms);//DataSetds=(DataSet)obj;//DataTabledt=ds.Tables[0];//returndt;//}//publicobjectDbDeCompressDs(byte[]temp)//{//byte[]da=(byte[])temp;//MemoryStreaminput=newMemoryStream();//input.Write(da,0,da.Length);//input.Position=0;//GZipStreamgzip=newGZipStream(input,CompressionMode.Decompress,true);//MemoryStreamoutput=newMemoryStream();//byte[]buff=newbyte[4096];//intread=-1;//read=gzip.Read(buff,0,buff.Length);//while(read>0)//{//output.Write(buff,0,read);//read=gzip.Read(buff,0,buff.Length);//}//gzip.Close();//byte[]rebytes=output.ToArray();//output.Close();//input.Close();//MemoryStreamms=newMemoryStream(rebytes);//BinaryFormatterbf=newBinaryFormatter();//objectobj=bf.Deserialize(ms);//DataSetds=(DataSet)obj;//returnds;//}///<summary>///动态加载DLL执行函数///</summary>///<paramname="dllname"></param>///<paramname="dllinfo"></param>///<paramname="command"></param>///<paramname="Params"></param>///<returns></returns>/////<summary>/////通过反射机制设置类属性值/////</summary>/////<paramname="name">属性名称</param>/////<paramname="Params">参数</param>//publicstaticvoidSetProperty(stringname,object[]Params)//{////设置属性值//Servicetmp=newService();//Typetype=tmp.GetType();//Objectobj=type.InvokeMember(null,//BindingFlags.DeclaredOnly|//BindingFlags.Public|BindingFlags.NonPublic|//BindingFlags.Instance|BindingFlags.CreateInstance,null,null,null);//type.InvokeMember(name,BindingFlags.SetProperty,null,obj,Params);//}publicstaticDataTableDeserializeDataTable(stringpXml){StringReaderstrReader=newStringReader(pXml);XmlReaderxmlReader=XmlReader.Create(strReader);XmlSerializerserializer=newXmlSerializer(typeof(DataTable));DataTabledt=serializer.Deserialize(xmlReader)asDataTable;returndt;}privatestaticstringSerializeDataTableXml(DataTablepDt){//序列化DataTableStringBuildersb=newStringBuilder();XmlWriterwriter=XmlWriter.Create(sb);XmlSerializerserializer=newXmlSerializer(typeof(DataTable));serializer.Serialize(writer,pDt);writer.Close();returnsb.ToString();}}
解决方案
解决方案二:
不明觉厉~~~~
解决方案三:
好长问题呢只是提问还是分享啊
解决方案四:
lz是个好人。顺便接分。