zoukankan      html  css  js  c++  java
  • 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>


  • 相关阅读:
    HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法
    如何制作一个没有任何窗体的,隐藏在后台的程序
    ActiveMQ持久化消息(转)
    ActiveMQ发布订阅模式(转)
    .Net平台下ActiveMQ入门实例(转)
    SQL重复记录查询的几种方法(转)
    XML 解析中,如何排除控制字符
    事件委托
    margin重叠
    office web apps 部署-搭建域控服务器
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6915547.html
Copyright © 2011-2022 走看看