从调用API接口 看.net framework和 net core的异同
1个回答
2017-09-14
展开全部
由于.netcore是跨平台使用的,所有微软重写好多API,由此有些差异也是正常的。现在就来看下请求API的差别吧。首先,原本的.netframework请求方法:复制代码publicstaticTHttpPostWithDecompression(stringrequest,stringurl,inttimeout)whereT:new(){varhttpWebRequest=(HttpWebRequest)WebRequest.Create(url);httpWebRequest.Proxy=null;httpWebRequest.Timeout=timeout;httpWebRequest.Method="POST";httpWebRequest.ContentType="application/json";httpWebRequest.AutomaticDecompression=DecompressionMethods.Deflate|DecompressionMethods.GZip;varbytes=Encoding.UTF8.GetBytes(request);StreamrequestStream=null;varresponseContent=string.Empty;try{requestStream=httpWebRequest.GetRequestStream();requestStream.Write(bytes,0,bytes.Length);varwebResponse=httpWebRequest.GetResponse();varstream=webResponse.GetResponseStream();if(stream!=null){varstreamReader=newStreamReader(stream);responseContent=streamReader.ReadToEnd();streamReader.Close();}webResponse.Close();returnJsonConvert.DeserializeObject(responseContent);}catch(Exceptionexception){returnnewT();}finally{if(requestStream!=null){requestStream.Close();}}}复制代码第一种返回错误编码的.netcore的写法:复制代码publicasyncstaticTaskHttpPostWithDecompression(stringrequest,stringurl,inttimeout)whereT:new(){varhttpWebRequest=(HttpWebRequest)WebRequest.Create(url);httpWebRequest.Proxy=null;httpWebRequest.ContinueTimeout=timeout;httpWebRequest.Method="POST";httpWebRequest.ContentType="application/json";httpWebRequest.Headers["Accept-Encoding"]="gzip,deflate";//httpWebRequest.AutomaticDecompression=DecompressionMethods.Deflate|DecompressionMethods.GZip;vardata=Encoding.UTF8.GetBytes(request);using(varrequestStream=awaithttpWebRequest.GetRequestStreamAsync())//释放{requestStream.Write(data,0,data.Length);}stringresult=string.Empty;//响应流using(varresponse=(HttpWebResponse)awaithttpWebRequest.GetResponseAsync()){StreamresponseStream=null;if(response.StatusCode==HttpStatusCode.OK){responseStream=response.GetResponseStream();if(responseStream!=null){varstreamReader=newStreamReader(responseStream,Encoding.UTF8);//获取返回的信息result=streamReader.ReadToEnd();returnJsonConvert.DeserializeObject(result);}}}returnnewT();}复制代码查看下.netcore下的属性和方法:可以明显发现比.netframwork下少了好多。也就是.netcore的请求类型,更趋于向浏览器的请求头靠拢,也就是使用Headers字典(.netframwork下也有,但是同层级也会有一些属性,比如采用压缩)。主要的区别就是响应流采用的是异步的方式,而.netframwork采用同步的方式,必须手动close(),而新的写法,就只能用using了。但是,上述的这个方法,害死人了,请求也成功,就是编码始终调试不对,请求各位大神指点一二。由于水平问题,未能解决上述问题,所以小弟采用.netcore的HttpClientHandler类进行http请求:get方法复制代码//////get请求//////url包含参数publicstaticasyncTaskdoGet(stringurl)whereT:new(){varhandler=newHttpClientHandler(){AutomaticDecompression=DecompressionMethods.GZip};using(varhttp=newHttpClient(handler)){varresponse=awaithttp.GetAsync(url);//确保HTTP成功状态值response.EnsureSuccessStatusCode();stringresponseStr=response.Content.ReadAsStringAsync().Result;returnJsonConvert.DeserializeObject(responseStr);}}复制代码post请求复制代码//////post请求//////url包含参数publicasyncstaticTaskHttpPostWithDecompression(stringrequest,stringapiUrl,inttimeout)whereT:new(){HttpClientHandlerhandler=newHttpClientHandler();handler.Proxy=null;handler.AutomaticDecompression=DecompressionMethods.GZip|DecompressionMethods.Deflate;vardata=Encoding.UTF8.GetBytes(request);using(varhttpClient=newHttpClient(handler)){httpClient.BaseAddress=newUri(apiUrl);httpClient.DefaultRequestHeaders.Accept.Add(newMediaTypeWithQualityHeaderValue("application/json"));varcont=newStringContent(request);//varcontent=newFormUrlEncodedContent(JsonConvert.DeserializeObject>(request));//被上面这个注释掉的代码,这种偷懒转Dic的方式给坑苦了,不能这么用啊!!!varresponse=awaithttpClient.PostAsync(apiUrl,cont);stringresult=response.Content.ReadAsStringAsync().Result;returnJsonConvert.DeserializeObject(result);}}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询