您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > Java > CHTTP两种请求get和post
【C#】HTTP请求GET,POSTHTTP定义了与服务器交互的不同方法,基本方法有GET,POST,PUT,DELETE,分别对于查,该,增,删。一般情况下我们只用到GET和POST,其他两种都也可以用GET和POST来实现,很多浏览器也只实现了GET和POST两种方法GET:用户获取信息安全的:不会对服务器上的数据进行修改,和破坏幂等的:相同的请求所返回的结果相同参数是通过Url来传输,接在Url后面中间用?分开,不同参数之间用&分开,下面表示传递两个参数param1和param2,值为value1和value2=value1¶m2=value2通过上面可以看到,GET请求数据在隐私方面又是不安全的,因为Url上的信息可以直接让用户看到,这样不利于隐私保护POST:用于更新数据传递的数据对用户隐藏,封装在包里,具体看演示有时候请求会重定向,但我们就需要从重定向url获取东西,这时可以设置重定向禁用,你就可以从headers的Location属性中获取重定向地址request.AllowAutoRedirect=false;string[]values=request.Headers.GetValues(Location);下面是辅助类的定义publicclassHttpHelper{///summary///创建GET方式的HTTP请求////summarypublicstaticHttpWebResponseCreateGetHttpResponse(stringurl,inttimeout,stringuserAgent,CookieCollectioncookies){HttpWebRequestrequest=null;if(url.StartsWith(https,StringComparison.OrdinalIgnoreCase)){//对服务端证书进行有效性校验(非第三方权威机构颁发的证书,如自己生成的,不进行验证,这里返回true)ServicePointManager.ServerCertificateValidationCallback=newRemoteCertificateValidationCallback(CheckValidationResult);request=WebRequest.Create(url)asHttpWebRequest;request.ProtocolVersion=HttpVersion.Version10;//http版本,默认是1.1,这里设置为1.0}else{request=WebRequest.Create(url)asHttpWebRequest;}request.Method=GET;//设置代理UserAgent和超时//request.UserAgent=userAgent;//request.Timeout=timeout;if(cookies!=null){request.CookieContainer=newCookieContainer();request.CookieContainer.Add(cookies);}returnrequest.GetResponse()asHttpWebResponse;}///summary///创建POST方式的HTTP请求////summarypublicstaticHttpWebResponseCreatePostHttpResponse(stringurl,IDictionarystring,stringparameters,inttimeout,stringuserAgent,CookieCollectioncookies){HttpWebRequestrequest=null;//如果是发送HTTPS请求if(url.StartsWith(https,StringComparison.OrdinalIgnoreCase)){//ServicePointManager.ServerCertificateValidationCallback=newRemoteCertificateValidationCallback(CheckValidationResult);request=WebRequest.Create(url)asHttpWebRequest;//request.ProtocolVersion=HttpVersion.Version10;}else{request=WebRequest.Create(url)asHttpWebRequest;}request.Method=POST;request.ContentType=application/x-设置代理UserAgent和超时//request.UserAgent=userAgent;//request.Timeout=timeout;if(cookies!=null){request.CookieContainer=newCookieContainer();request.CookieContainer.Add(cookies);}//发送POST数据if(!(parameters==null||parameters.Count==0)){StringBuilderbuffer=newStringBuilder();inti=0;foreach(stringkeyinparameters.Keys){if(i0){buffer.AppendFormat(&{0}={1},key,parameters[key]);}else{buffer.AppendFormat({0}={1},key,parameters[key]);i++;}}byte[]data=Encoding.ASCII.GetBytes(buffer.ToString());using(Streamstream=request.GetRequestStream()){stream.Write(data,0,data.Length);}}string[]values=request.Headers.GetValues(Content-Type);returnrequest.GetResponse()asHttpWebResponse;}///summary///获取请求的数据////summarypublicstaticstringGetResponseString(HttpWebResponsewebresponse){using(Streams=webresponse.GetResponseStream()){StreamReaderreader=newStreamReader(s,Encoding.UTF8);returnreader.ReadToEnd();}}///summary///验证证书////summaryprivatestaticboolCheckValidationResult(objectsender,X509Certificatecertificate,X509Chainchain,SslPolicyErrorserrors){if(errors==SslPolicyErrors.None)returntrue;returnfalse;}}
本文标题:CHTTP两种请求get和post
链接地址:https://www.777doc.com/doc-2905751 .html