最近在弄一些第三方的平台,经常调用第三方的接口实现某些特定的功能

在实现的同时基本上都需要本地的数据经过服务器在Request到第三方的服务器中处理,再返回相应的数据结构体:json/xml

以下是我总结的一个小方法,请农友们笑纳:

public static string postWebReq(string PostUrl, string ParamData, Encoding DataEncode)
    {
      string ret = string.Empty;
      try
      {
        byte[] byteArray = DataEncode.GetBytes(ParamData);
        HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(PostUrl));
        webReq.Method = "POST";
        webReq.ContentType = "application/x-www-form-urlencoded";
        webReq.ContentLength = byteArray.Length;

        Stream newStream = webReq.GetRequestStream();
        newStream.Write(byteArray, 0, byteArray.Length);
        newStream.Close();

        HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
        StreamReader sr = new StreamReader(response.GetResponseStream(), DataEncode);
        ret = sr.ReadToEnd();

        sr.Close();
        response.Close();
        newStream.Close();
      }
      catch (WebException ex)
      {
        Log.WriteLog(LogFile.Error, ex.Message);
      }
      finally
      {
        Log.WriteLog(LogFile.Info, ret);
      }
      return ret;
    }
您可能感兴趣的文章:

  • asp.net连接数据库读取数据示例分享
  • asp.net 通用的连接数据库实例代码
  • asp.net连接数据库 增加,修改,删除,查询代码
  • ASP.NET中操作SQL数据库(连接字符串的配置及获取)
  • asp.net获得数据控件事件索引并获取其中值总结
  • asp.net获取SQL所有数据库名、所有表名、所有字段名
  • ASP.NET连接数据库并获取数据方法总结
声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。