/// <summary>
/// get方式访问webapi
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string httpGet(string url)
{
try
{
HttpWebRequest MyRequest = (HttpWebRequest)WebRequest.Create(url);
MyRequest.Method = "GET";
MyRequest.Accept = "application/json";
//返回类型可以为
//1、application/json
//2、text/json
//3、application/xml
//4、text/xml

MyRequest.ContentType = "application/json";
//上传类型是能为json

string retData = null;//接收返回值
HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse();
if (MyResponse.StatusCode == HttpStatusCode.OK)
{
Stream MyNewStream = MyResponse.GetResponseStream();
StreamReader MyStreamReader = new StreamReader(MyNewStream, Encoding.UTF8);
retData = MyStreamReader.ReadToEnd();
MyStreamReader.Close();
}
MyResponse.Close();
return retData;
}
catch (Exception ex)
{
return ex.Message;
}
}

/// <summary>
/// post方式访问webapi
/// </summary>
/// <param name="url"></param>
/// <param name="postdata"></param>
/// <returns></returns>
public static string httpPost(string url, string postdata)
{
try
{
HttpWebRequest MyRequest = (HttpWebRequest)WebRequest.Create(url);
MyRequest.Method = "POST";
MyRequest.Accept = "application/json";
//返回类型可以为
//1、application/json
//2、text/json
//3、application/xml
//4、text/xml

MyRequest.ContentType = "application/json";
//上传类型是能为json

if (postdata != null)
{
ASCIIEncoding MyEncoding = new ASCIIEncoding();
byte[] MyByte = MyEncoding.GetBytes(postdata);
Stream MyStream = MyRequest.GetRequestStream();
MyStream.Write(MyByte, 0, postdata.Length);
MyStream.Close();
}

string retData = null;//返回值
HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse();
if (MyResponse.StatusCode == HttpStatusCode.OK)
{
Stream MyNewStream = MyResponse.GetResponseStream();
StreamReader MyStreamReader = new StreamReader(MyNewStream, Encoding.UTF8);
retData = MyStreamReader.ReadToEnd();
MyStreamReader.Close();
}
MyResponse.Close();
return retData;
}
catch (Exception ex)
{
return ex.Message;
}
}

最新文章

  1. Java开源库
  2. spark - tasks is bigger than spark.driver.maxResultSize
  3. wordpress钩子和钩子函数
  4. jquery 清空 iframe 的内容,,iframe自适应高度
  5. 移动端事件touchstart、touchmove、touchend
  6. 在ubuntu上搭建开发环境7---ubuntu安装JDK
  7. 【代码分享】简单html5足球射门游戏分享
  8. oracle时间格式转换问题 ORA-01810: format code appears twice--转
  9. php mysql 数据库写入与读取取文件
  10. 自己定义View----点击滑动选择字母列表
  11. .NET 跨平台界面框架和为什么你首先要考虑再三
  12. window.showModalDialog
  13. springcloud~服务注册与发现Eureka的使用
  14. 如何用浏览器在线查看.ipynb文件
  15. Oracle中对XMLType的简单操作(extract、extractvalue...)
  16. Spring IoC和AOP使用扩展
  17. webservice:com.sun.xml.internal.ws.server.ServerRtException: [failed to localize]
  18. 处理:&ldquo;ORA-00257: archiver error. Connect internal only, until freed&rdquo;的错误问题
  19. java将所有的字符串转换为大写或小写
  20. P1044-栈-洛谷luogu

热门文章

  1. 微信小程序全局变量改变监听
  2. java中String和int相互转换常用方法详解
  3. Redis复制流程:图解
  4. 云开发新能力,支持 HTTP 调用 API
  5. ASP.NET--Web服务器端控件和Html控件
  6. Netty-Pipeline深度解析
  7. CSingleLock
  8. dubbo框架设计学习
  9. 宽度总结-scrollWidth,clientWidth,offectWidth
  10. [leetcode] 406. Queue Reconstruction by Height (medium)