【问题标题】:Get real-time data from a http long lived web service从 http 长寿命 Web 服务获取实时数据
【发布时间】:2012-07-18 03:57:19
【问题描述】:

我有一个长期存在的 http 网络服务。如果它有新数据,它将使用 http GET 推送到客户端。如何使用 HttpWebRequest c# 从 http 长寿命 Web 服务接收实时数据?

【问题讨论】:

    标签: c# httpwebrequest


    【解决方案1】:

    如果你想使用Get获取数据,你可以使用这个(使用GetResponse时响应是同步的):

     public string GetMessageViaGet(string endPoint)
            {
                HttpWebRequest request = CreateWebRequest(endPoint);
    
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    var responseValue = string.Empty;
    
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        string message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
                        throw new ApplicationException(message);
                    }
    
                    // grab the response  
                    using (var responseStream = response.GetResponseStream())
                    {
                        using (var reader = new StreamReader(responseStream))
                        {
                            responseValue = reader.ReadToEnd();
                        }
                    }
    
                    return responseValue;
                }
            }
            private HttpWebRequest CreateWebRequest(string endPoint)
            {
                var request = (HttpWebRequest)WebRequest.Create(endPoint);
    
                request.Method = "GET";
                request.ContentLength = 0;
                request.ContentType = "text/xml";
                return request;
            }
    

    【讨论】:

      【解决方案2】:

      如果您想通过 post 获取数据,请执行此操作

         public string GetMessageViaPost(string endPoint, string paramtersJson)
              {
                  string responseValue;
                  byte[] bytes = Encoding.UTF8.GetBytes(paramtersJson);
      
                  HttpWebRequest request = CreateWebRequest(endPoint, bytes.Length);
      
                  using (var requestStream = request.GetRequestStream())
                  {
                      requestStream.Write(bytes, 0, bytes.Length);
                  }
      
                  using (var response = (HttpWebResponse)request.GetResponse())
                  {
                      if (response.StatusCode != HttpStatusCode.OK)
                      {
                          string message = String.Format("POST failed. Received HTTP {0}", response.StatusCode);
                          throw new ApplicationException(message);
                      }
                      // grab the response  
                      using (var responseStream = response.GetResponseStream())
                      {
                          using (var reader = new StreamReader(responseStream))
                          {
                              responseValue = reader.ReadToEnd();
                          }
                      }
                  }
      
                  return responseValue;
              }
      
              private HttpWebRequest CreateWebRequest(string endPoint, Int32 contentLength)
              {
                  var request = (HttpWebRequest)WebRequest.Create(endPoint);
      
                  request.Method = "POST";
                  request.ContentLength = contentLength;
                  request.ContentType = "application/json";// "application/x-www-form-urlencoded";
      
                  return request;
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-24
        • 2017-04-11
        • 1970-01-01
        相关资源
        最近更新 更多