• c# POST和GET方式通过server地址提交数据


    1:POST方式提交:

    <strong><span style="font-size:14px;">private static string HttpPostConnectToServer(string serverUrl, string postData)
            {
                var dataArray = Encoding.UTF8.GetBytes(postData);
                //创建请求
                var request = (HttpWebRequest)HttpWebRequest.Create(serverUrl);
                request.Method = "POST";
                request.ContentLength = dataArray.Length;
                //设置上传服务的数据格式
                request.ContentType = "application/x-www-form-urlencoded";
                //请求的身份验证信息为默认
                request.Credentials = CredentialCache.DefaultCredentials;
                //请求超时时间
                request.Timeout = 10000;
                //创建输入流
                Stream dataStream;
                //using (var dataStream = request.GetRequestStream())
                //{
                //    dataStream.Write(dataArray, 0, dataArray.Length);
                //    dataStream.Close();
                //}
                try
                {
                    dataStream = request.GetRequestStream();
                }
                catch (Exception)
                {
                    return null;//连接服务器失败
                }
                //发送请求
                dataStream.Write(dataArray, 0, dataArray.Length);
                dataStream.Close();
                //读取返回消息
                string res;
                try
                {
                    var response = (HttpWebResponse)request.GetResponse();
                    var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    res = reader.ReadToEnd();
                    reader.Close();
                }
                catch (Exception ex)
                {
                    var result = new ServerResult();
                    return "{"error":"connectToServer","error_description":"" + ex.Message + ""}";//连接服务器失败
                }
                return res;
            }</span></strong>

    2:GET方式提交:

    <strong><span style="font-size:14px;">private static string HttpGetConnectToServer(string serverUrl, string postData)
            {
                //创建请求
                var request = (HttpWebRequest)HttpWebRequest.Create(serverUrl + "?" + postData);
                request.Method = "GET";
                //设置上传服务的数据格式
                request.ContentType = "application/x-www-form-urlencoded";
                //请求的身份验证信息为默认
                request.Credentials = CredentialCache.DefaultCredentials;
                //请求超时时间
                request.Timeout = 10000;
                //读取返回消息
                string res;
                try
                {
                    var response = (HttpWebResponse)request.GetResponse();
                    var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    res = reader.ReadToEnd();
                    reader.Close();
                }
                catch (Exception ex)
                {
                    var result = new ServerResult();
                    return "{"error":"connectToServer","error_description":"" + ex.Message + ""}";
                }
                return res;
            }</span></strong>

    3:调用方法:

    <strong><span style="font-size:14px;">//服务地址
        var serverUrl ="https://openapi.baidu.com/rest/2.0/devapi/v1/lightapp/query/isonline";
     //上传的数据
        var postData = string.Format("access_token={0}&offline_app_id={1}", token, appId);</span></strong>


  • 相关阅读:
    关于locals()、globals()以及作用域的一些感悟
    Python中创建对象的方法
    Python之__loader__
    tag上、push上和pull 取Docker 映像
    制作Docker镜像
    在Docker Hub上查找可用的Image映像
    window下安装mysql
    linux下安装python3
    yun、apt、wget的区别
    红帽7 Squid部署代理服务
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6915547.html
Copyright © 2020-2023  润新知