• winform访问url传参有返回值


    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://。。。。");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string ret = string.Empty;
                Stream s;
                string StrDate = "";
                string strValue = "";
     
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    s = response.GetResponseStream();
                    ////在这儿处理返回的文本就OK了,如:
     
                    StreamReader Reader = new StreamReader(s, Encoding.UTF8);
                    while ((StrDate = Reader.ReadLine()) != null)
                    {
                        strValue += StrDate + " ";
                    }  
                }
    Console.WriteLine(strValue);
    Console.ReadKey();

    }
    /*
    Winform传递参数至Url并获得返回值

    在winform里面调用URL并传递参数主要使用HttpWebRequest对象,分为两种形式post和get,个人感觉就是模拟了浏览器,

    1.post方法:

    /// <summary>
    /// 使用post方法,调用短信接口
    /// </summary>
    /// <param name="PhoneNumber"></param>
    /// <param name="SmsContent"></param>
    /// <param name="PhoneNumberType"></param>
    /// <param name="SmsUser"></param>
    private void CallMsgCenterToSendMsgPost(string PhoneNumber, string SmsContent, string PhoneNumberType, string SmsUser)
    {
    string formUrl = ConfigurationSettings.AppSettings["formUrl"].ToString().Trim();//url地址
    string formData = "PhoneNumber=" + PhoneNumber + "&SmsContent=" +SmsContent + "&PhoneNumberType=" + PhoneNumberType + "&SmsUser=" + SmsUser + "";
    CookieContainer cookieContainer = new CookieContainer();
    // 将提交的字符串数据转换成字节数组
    byte[] postData = Encoding.UTF8.GetBytes(formData);

    // 设置提交的相关参数
    HttpWebRequest request = WebRequest.Create(formUrl) as HttpWebRequest;
    Encoding myEncoding = Encoding.GetEncoding("gb2312");
    request.Method = "POST";
    request.KeepAlive = false;
    request.AllowAutoRedirect = true;
    request.ContentType = "application/x-www-form-urlencoded";
    request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
    request.CookieContainer = cookieContainer;
    request.ContentLength = postData.Length;

    // 提交请求数据
    System.IO.Stream outputStream = request.GetRequestStream();
    outputStream.Write(postData, 0, postData.Length);
    outputStream.Close();

    HttpWebResponse response;
    Stream responseStream;
    StreamReader reader;
    string srcString;
    response = request.GetResponse() as HttpWebResponse;
    responseStream = response.GetResponseStream();
    reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
    srcString = reader.ReadToEnd();
    reader.Close();
    }

    2,get方法

    /// <summary>
    /// 使用get方法
    /// </summary>
    /// <param name="PhoneNumber"></param>
    /// <param name="SmsContent"></param>
    /// <param name="PhoneNumberType"></param>
    /// <param name="SmsUser"></param>
    private void CallMsgCenterToSendMsgGet(string PhoneNumber, string SmsContent, string PhoneNumberType, string SmsUser)
    {
    string formUrl = ConfigurationSettings.AppSettings["formUrl"].ToString().Trim();
    string formData = "PhoneNumber=" + HttpUtility.UrlEncode(PhoneNumber) + "&SmsContent=" + HttpUtility.UrlEncode(SmsContent) + "&PhoneNumberType=" + HttpUtility.UrlEncode(PhoneNumberType) + "&SmsUser=" + HttpUtility.UrlEncode(SmsUser) + "";
    CookieContainer cookieContainer = new CookieContainer();

    formUrl = formUrl + "?" + formData;
    // 设置提交的相关参数
    HttpWebRequest request = WebRequest.Create(formUrl) as HttpWebRequest;

    request.Method = "GET";
    request.KeepAlive = false;
    request.AllowAutoRedirect = true;
    request.ContentType = "application/x-www-form-urlencoded";
    request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
    request.CookieContainer = cookieContainer;


    HttpWebResponse SendSMSResponse = (HttpWebResponse)request.GetResponse();


    StreamReader SendSMSResponseStream = new StreamReader(SendSMSResponse.GetResponseStream());

    string strRespone = SendSMSResponseStream.ReadToEnd();

    SendSMSResponse.Close();
    SendSMSResponseStream.Close();
    }

    3,调用起来都比较简单的,主要是在传递参数的过程中注意中文的处理,否则会变为乱码,获取返回值当然就使用HttpWebResponse对象了
    */
    }
    }

  • 相关阅读:
    VS2005 GridView操作大全(转载)
    架构与模式
    JS全选与取消
    C#查找指定文件夹下指定后缀名的所有文件
    select poll epoll Hello
    scanf() gets() fgets()使用注意事项 Hello
    gtk_init() Hello
    用C实现FFT算法 Hello
    时间相关函数 Hello
    gcc的编译属性和选项 Hello
  • 原文地址:https://www.cnblogs.com/unintersky/p/3875112.html
Copyright © 2020-2023  润新知