.net - Java封装httpGet方法和C#封装httpGet入门级别的问题 | 代码转化:Java -》 C#
巴扎黑
巴扎黑 2017-04-17 14:50:55
0
3
429

我需要c# 用httpclient实现的例子。 system.net.http.

 //public static HttpClientResult httpGet(string url, IDictionary<string, string> headMap)
        public static string httpGet(string url, IDictionary<string, string> headMap)
        {
            string response = "";
            HttpGet httpGet = null;
            CloseableHttpResponse ht = null;
            try
            {
                httpGet = new HttpGet(url);

                if (headMap != null && headMap.Count > 0)
                {
                    foreach (string name in headMap.Keys)
                    {
                        httpGet.addHeader(name, headMap[name]);
                    }
                }

                CloseableHttpClient hc = HttpClients.createDefault();
                RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build(); //设置请求和传输超时时间
                httpGet.Config = requestConfig;
                ht = hc.execute(httpGet);
                 
                HttpEntity het = ht.Entity;
                InputStream @is = het.Content;
                BufferedReader br = new BufferedReader(new InputStreamReader(@is, "utf8"));
                string readLine;
                while ((readLine = br.readLine()) != null)
                {
                    response = response + readLine;
                }
                @is.close();
                br.close();
                int status = ht.StatusLine.StatusCode;
                //return new HttpClientResult(status, response);
                return null;
            }
            catch (Exception e)
            {
                throw new HttpClientException(e);
            }
            finally
            {
                if (httpGet != null)
                {
                    httpGet.releaseConnection();
                } 
                if (ht != null)
                {
                    try
                    {
                        ht.close();
                    }
                    catch (IOException)
                    {
                    }
                }
            }
        }

这是一段java里封装httpget的方法,有没有人能帮忙翻译成C#相应的代码,让我作为示例代码,学习一下。我是做java的,对C#非常不熟。

大神辛苦了,这个问题对大神来说肯定很简单,只要核心代码翻译一下就好,异常啥的就不要啦,最好C#流的常用类也能指点下

巴扎黑
巴扎黑

répondre à tous(3)
左手右手慢动作
 public static HttpClientResult httpGet(string url, IDictionary<string, string> headMap)
        {
            string response = "";
     
            try
            {
                HttpClient hc = new HttpClient();
                hc.Timeout = new System.TimeSpan(300);

                if (headMap != null && headMap.Count > 0)
                {
                    foreach (string name in headMap.Keys)
                    {
                        hc.DefaultRequestHeaders.Add(name,headMap[name]);
                    }
                }

                var getTask =  hc.GetAsync(new Uri(url));
                HttpResponseMessage responseM = hc.GetAsync(new Uri(url)).Result;

                Stream instream = responseM.Content.ReadAsStreamAsync().Result;
                BufferedStream bfs = new BufferedStream(instream);
                byte[] buffer = new byte[1024];
                StringBuilder sb = new StringBuilder();
                while (bfs.Read(buffer, 0, buffer.Length) > 0)
                {
                    sb.Append(Encoding.GetEncoding("UTF-8").GetString(buffer));
                }
                response = sb.ToString();

                bfs.Flush();
                bfs.Close();
                instream.Close();

                int statusCode = (int)responseM.StatusCode;
                return new HttpClientResult(statusCode, response);
            }
            catch (HttpRequestException e)
            {
                throw new HttpRequestException("HttpRequestException");
            }
            finally
            {
               
            }
        }
伊谢尔伦

链接描述

巴扎黑

执行HTTP GET请求。

https://baidupcs.svn.codeplex.com/svn/src/BaiduSdk/BaiduSdk/Util/WebUtils.cs

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!