zoukankan      html  css  js  c++  java
  • C# 发起Get和Post请求

    public class ApiHelper
        {
         //contentType application/json or application/xml
    public string HttpGet(string Url, string contentType) { try { string retString = string.Empty; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "GET"; request.ContentType = contentType; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(myResponseStream); retString = streamReader.ReadToEnd(); streamReader.Close(); myResponseStream.Close(); return retString; } catch (Exception ex) { throw ex; } } public static string HttpPost(string Url, string postDataStr, string contentType, out bool isOK) { string retString = string.Empty; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "POST"; request.ContentType = contentType; request.Timeout = 600000;//设置超时时间 request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr); Stream requestStream = request.GetRequestStream(); StreamWriter streamWriter = new StreamWriter(requestStream); streamWriter.Write(postDataStr); streamWriter.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(responseStream); retString = streamReader.ReadToEnd(); streamReader.Close(); responseStream.Close(); isOK = true; } catch (Exception ex) { if (ex.GetType() == typeof(WebException))//捕获400错误 { var response = ((WebException)ex).Response; Stream responseStream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(responseStream); retString = streamReader.ReadToEnd(); streamReader.Close(); responseStream.Close(); } else { retString = ex.ToString(); } isOK = false; } return retString; } }
  • 相关阅读:
    在AS/400上根据日期生成星期几
    如何删除含无效字符的文件
    在CL中使用SST或者SUBSTRING
    取网络属性
    如何在程序中获取系统ASP使用率等系统状态信息
    在CL中使用ELSE
    在CL中读一个文件
    如何在FTP命令行执行AS/400命令
    广告悬停功能
    关于Grouping, Rollup,cube,
  • 原文地址:https://www.cnblogs.com/nicopoiduang/p/10818374.html
Copyright © 2011-2022 走看看