问题描述
PublicFunctionpostCaptcha(ByValstrApiKeyAsString,ByValstrCaptchaFileAsString)AsStringDimobjEncodingAsNewSystem.Text.UTF8EncodingDimobjStreamWriterAsSystem.IO.StreamWriterDimobjStreamAsSystem.IO.StreamDimobjHTTPRequestAsHttpWebRequestDimsbPostDataAsNewSystem.Text.StringBuilderDimintUploadBitAsIntegerDimintUploadSoFarAsIntegerDiminttoUploadAsIntegerDimiAsIntegerDimobjStreamReaderAsSystem.IO.StreamDimstrResultAsString'setrequestpropertiesobjHTTPRequest=System.Net.WebRequest.Create("http://www.captcha4u.com/api.aspx")objHTTPRequest.AllowAutoRedirect=TrueobjHTTPRequest.Accept="*/*"objHTTPRequest.UserAgent="Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;.NETCLR1.0.3705)"objHTTPRequest.KeepAlive=FalseobjHTTPRequest.Timeout=30000objHTTPRequest.Method="POST"objHTTPRequest.ContentType="multipart/form-data;boundary=7cf2a327f01ae"sbPostData.Append("--"+"7cf2a327f01ae"+vbCrLf)sbPostData.Append("Content-Disposition:form-data;name=""api_key"""+vbCrLf)sbPostData.Append(vbCrLf)sbPostData.Append(strApiKey+vbCrLf)sbPostData.Append("--"+"7cf2a327f01ae"+vbCrLf)sbPostData.Append("Content-Disposition:form-data;name=""expire"""+vbCrLf)sbPostData.Append(vbCrLf)sbPostData.Append("1000"+vbCrLf)'defaultedto1000butyoucanchangethissbPostData.Append("--"+"7cf2a327f01ae"+vbCrLf)sbPostData.Append("Content-Disposition:form-data;name=""method"""+vbCrLf)sbPostData.Append(vbCrLf)sbPostData.Append("upload_captcha"+vbCrLf)sbPostData.Append("--"+"7cf2a327f01ae"+vbCrLf)sbPostData.Append("Content-Disposition:form-data;name=""rights"""+vbCrLf)sbPostData.Append(vbCrLf)sbPostData.Append("false"+vbCrLf)'defaultedtofalsebutyoucanchangethis'thisistheheaderforourcaptchafileuploadsbPostData.Append("--"+"7cf2a327f01ae"+vbCrLf)sbPostData.Append("Content-Disposition:form-data;name=""file"";filename="""&strCaptchaFile&""+vbCrLf)sbPostData.Append("Content-Type:image/pjpeg"+vbCrLf)sbPostData.Append(vbCrLf)'readourcaptchintoabytearrayDimobjBinReaderAsNewBinaryReader(File.OpenRead(strCaptchaFile))DimbytPhotoContentsAsByte()=objBinReader.ReadBytes(objBinReader.BaseStream.Length)objBinReader.Close()'convertourotherpostdataintoabytearrayDimbytPostContentsAsByte()=objEncoding.GetBytes(sbPostData.ToString)'createafooterforinsertationafterthefilebytesareuploadedDimbytPostFooterAsByte()=objEncoding.GetBytes(vbCrLf+"--"+"7cf2a327f01ae"+vbCrLf)'createanewdatabuffertoholdallofourbytearraysDimbytDataBufferAsByte()=NewByte(bytPostContents.Length+bytPhotoContents.Length+bytPostFooter.Length){}'copythecontentsofourthreebytearraysintooursinglebytearraySystem.Buffer.BlockCopy(bytPostContents,0,bytDataBuffer,0,bytPostContents.Length)System.Buffer.BlockCopy(bytPhotoContents,0,bytDataBuffer,bytPostContents.Length,bytPhotoContents.Length)System.Buffer.BlockCopy(bytPostFooter,0,bytDataBuffer,bytPostContents.Length+bytPhotoContents.Length,bytPostFooter.Length)'setthecontentlengthbasedonournewbytearraylengthobjHTTPRequest.ContentLength=bytDataBuffer.Length'getourstreamandpostourdataobjStream=objHTTPRequest.GetRequestStream()'chunkupourdataanduploadittoourstream'willgenerallyonlyneedtosendinonechunkunlessfileislargeintUploadBit=Math.Max(bytDataBuffer.Length/100,50*1024)intUploadSoFar=0Whilei<bytDataBuffer.LengthinttoUpload=Math.Min(intUploadBit,bytDataBuffer.Length-i)intUploadSoFar+=inttoUploadobjStream.Write(bytDataBuffer,i,inttoUpload)i=i+intUploadBitEndWhile'closeourstreamobjStream.Close()'gettheresponsefromtheserverDimobjHTTPResponseAsHttpWebResponse=CType(objHTTPRequest.GetResponse(),HttpWebResponse)objStreamReader=objHTTPResponse.GetResponseStream()'finalresultfromserverisreturnedtostrResultDimobjStreamResultAsNewSystem.IO.StreamReader(objStreamReader)strResult=objStreamResult.ReadToEnd'closeourobjectsobjStreamReader.Close()objStreamResult.Close()ReturnstrResultEndFunction//以下为调用源码PrivateSubbtnPostCaptcha_Click(ByValsenderAsObject,ByValeAsSystem.EventArgs)HandlesbtnPostCaptcha.ClickIftxtAPIKey.Text=""OrtxtCaptchaImage.Text=""ThenMsgBox("APIKeyandCaptchaImagefilemustbespecified")ExitSubEndIftxtCaptchaID.Text=postCaptcha(txtAPIKey.Text,txtCaptchaImage.Text)'stripoutoursucessindicatortojustgetthecaptchaIDIfInStr(txtCaptchaID.Text,"SUCCESS:captcha_id=",CompareMethod.Text)>0ThentxtCaptchaID.Text=Replace(txtCaptchaID.Text,"SUCCESS:captcha_id=","")txtCaptchaID.Text=Replace(txtCaptchaID.Text,vbCr,"")txtCaptchaID.Text=Replace(txtCaptchaID.Text,vbLf,"")cmdGetResult.Enabled=TrueElseMsgBox("Erroruploadingcaptcha.APIResult:"&txtCaptchaID.Text)cmdGetResult.Enabled=FalseEndIfEndSub
解决方案
解决方案二:
nalresultfromserverisreturnedtostrResultDimobjStreamResultAsNewSystem.IO.StreamReader(objStreamReader,Encoding.GetEncoding("GB2312"))strResult=objStreamResult.ReadToEnd指定格式就可以了
解决方案三:
谢谢你,但是在asp.net是怎么还原的呢?????是用request.form["file"].tostring()这这样吗?