zoukankan      html  css  js  c++  java
  • .net 调用java rest ful api 实例

    注意post的参数组合

    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    request.Method = "POST";
    request.KeepAlive = true;
    request.AllowAutoRedirect = false;
    request.ContentType = "application/x-www-form-urlencoded";
    byte[] postdatabtyes = Encoding.UTF8.GetBytes("param={"imsi":"460023887603775","session":"hwJPCFmgNRtyOED91iTwpteCY"}");
    request.ContentLength = postdatabtyes.Length;
    Stream requeststream = request.GetRequestStream();
    requeststream.Write(postdatabtyes, 0, postdatabtyes.Length);
    requeststream.Close();
    string resp;
    try{
       using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
       {
          StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
          resp = sr.ReadToEnd();
        }
       }

    另外一种:

    2、以Post方式获取
    
      using System.Web;
    
      Uri address = new Uri("http://api.search.yahoo.com/ContentAnalysisService/V1/termExtraction");
    
      // Create the web request
      HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
    
      // Set type to POST
      request.Method = "POST";
      request.ContentType = "application/x-www-form-urlencoded";
    
      // Create the data we want to send
      string appId = "YahooDemo";
      string context = "Italian sculptors and painters of the renaissance"
      + "favored the Virgin Mary for inspiration";
      string query = "madonna";
    
      StringBuilder data = new StringBuilder();
      data.Append("appid=" + HttpUtility.UrlEncode(appId));
      data.Append("&context=" + HttpUtility.UrlEncode(context));
      data.Append("&query=" + HttpUtility.UrlEncode(query));
    
      // Create a byte array of the data we want to send
      byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
    
      // Set the content length in the request headers
      request.ContentLength = byteData.Length;
    
      // Write data
      using (Stream postStream = request.GetRequestStream())
      {
      postStream.Write(byteData, 0, byteData.Length);
      }
    
      // Get response
      using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
      {
      // Get the response stream
      StreamReader reader = new StreamReader(response.GetResponseStream());
    
      // Console application output
      Console.WriteLine(reader.ReadToEnd());
      }
      三、HTTP 验证请求
    
      有些朋友在调用Restful Api时,可能要提供用户名和密码进行Http身份验证,这里我们可以通过为请求增加一个 NetworkCredentials 实例来完成.
    
      // Create the web request
      HttpWebRequest request
      = WebRequest.Create("https://api.del.icio.us/v1/posts/recent") as HttpWebRequest;
    
      // Add authentication to request
      request.Credentials = new NetworkCredential("username", "password");
    
      // Get response
      using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
      {
      // Get the response stream
      StreamReader reader = new StreamReader(response.GetResponseStream());
    
      // Console application output
      Console.WriteLine(reader.ReadToEnd());
      }
  • 相关阅读:
    SQL SERVER 查询性能优化——分析事务与锁(一)
    SQL Server 查询性能优化——索引与SARG(三)
    SQL Server 查询性能优化——索引与SARG(二)
    SQL SERVER的锁机制(四)——概述(各种事务隔离级别发生的影响)
    SQL SERVER的锁机制(一)——概述(锁的种类与范围)
    SQL SERVER的锁机制(三)——概述(锁与事务隔离级别)
    关于AutoResetEvent的使用
    ASP.NET缓存
    Spring Framework 3.2.1 发布
    ExactImage 0.8.8 发布,C++ 图像处理库
  • 原文地址:https://www.cnblogs.com/lijianhua/p/5617250.html
Copyright © 2011-2022 走看看