zoukankan      html  css  js  c++  java
  • 【原创】C#使用HttpWebRequest,HttpWebResponse

    HttpWebRequest类:提供WebRequest类的Http特定的实现。HttpWebRequest 类对 WebRequest 中定义的属性和方法提供支持,也对使用户能够直接与使用 HTTP 的服务器交互的附加属性和方法提供支持。   

            /// <summary>
            /// 创建 HttpWebRequest
            /// </summary>
            /// <returns>HttpWebRequest</returns>
            public HttpWebRequest Create(string url,string name,string password)
            {

                //创建HttpWebRequest  实例,使用WebRequest.Create
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                //发送请求
                webRequest.Method = "POST";
                //编码
                webRequest.ContentType = "text/xml;charset=UTF-8";
                //访问凭证
                webRequest.Credentials = new NetworkCredential(name, password);
                //设置代理,如不需要代理,以下代码不需要
                string Isproxy = "代理ip";
                string proxyName = “代理访问用户”;
                string proxyPassword = “代理访问密码”;
                if (!string.IsNullOrEmpty(Isproxy))
                {
                    WebProxy myProxy = new WebProxy(Isproxy , true);
                    myProxy.Credentials = new NetworkCredential(proxyName , proxyPassword , "ip");
                    webRequest.Proxy = Isproxy ;
                }
                return webRequest;
            }

    提示:HttpWebRequest对象有一些相关设置属性,如Method(发送方式),TimeOut(请求超时时间),ContentType(Http标头的值)等等。不要使用构造函数创建HttpWebRequest实例,应使用WebRequest.Create(URI uriString)来创建实例,如果URI是Http://或Https://, 返回的是HttpWebRequest对象。(建立请求特定URI的对象) 当向资源发送数据时,GetRequestStream方法返回用于发送数据的Stream对象。

            /// <summary>
            /// <param name="value">发送数据</param>
            /// </summary>
            /// <returns>返回数据</returns>
            public string ResponseXML(string url,string name,string password,string value)
            {
                string result = "";
                //创建服务
                HttpWebRequest webRequest = Create(url,name,password);
                //字符转字节
                byte[] bytes = Encoding.UTF8.GetBytes(value);
                Stream writer = webRequest.GetRequestStream();
                writer.Write(bytes, 0, bytes.Length);
                writer.Flush();
                writer.Close();
                //返回 HttpWebResponse
                HttpWebResponse hwRes= webRequest.GetResponse() as HttpWebResponse;
                if (hwRes.StatusCode == System.Net.HttpStatusCode.OK)
                {//是否返回成功
                    Stream rStream = hwRes.GetResponseStream();
                    //流读取
                    StreamReader sr = new StreamReader(rStream, Encoding.UTF8);
                    result = sr.ReadToEnd();
                    sr.Close();
                    rStream.Close();
                }
                else
                {
                    result = "连接错误";
                }

               //关闭
                hwRes.Close();
                return result;
            }

  • 相关阅读:
    怎样在UIViewController的生命周期函数中判断是push或者是pop触发的生命周期函数
    配环境
    assert 断言
    mysql:创建新库,新表,查看character
    Python中的[...]是什么?
    同时安装了python3.4和python3.5,如何使用pip?
    亲测可用的优雅的在已经安装了python的Ubuntu上安装python3.5
    如何截网页长图?
    安装tensorflow
    unable to lock the administration directory (/var/lib/dpkg/) is another process using it
  • 原文地址:https://www.cnblogs.com/zhxhdean/p/2362983.html
Copyright © 2011-2022 走看看