Java封装httpGet方法和C#封装httpGet入门级别的问题 | 代码转化:Java -》 C#


我需要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#流的常用类也能指点下

get java .net c# httpclient

飞一般的疯猫 9 years, 6 months ago
mj1986 answered 9 years, 6 months ago

ZakuMan answered 9 years, 6 months ago


 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
            {

            }
        }

鱼丸子2313 answered 9 years, 6 months ago

Your Answer