zoukankan      html  css  js  c++  java
  • 常用 Http 的请求方法

    第一次用markdown写博客,体验下

    因为常用Http的几种请求方式,总结一下。

    1.封装两个Http的最常用方法,叫做HttpHelper类。

    HttpPost:

        public static string HttpPost(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postDataStr.Length;
    
            StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
            writer.Write(postDataStr);
            writer.Flush();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string encoding = response.ContentEncoding;
    
            if (encoding == null || encoding.Length < 1)
            {
                encoding = "UTF-8"; //默认编码  
            }
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
            string retString = reader.ReadToEnd();
            return retString;
        }
    

    HttpGet:

        public static string HttpGet(string Url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
    
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
    
            return retString;
        }
    

    WebClientde中Post和Get:

    	//Post
    	using (var client = new WebClient())
    	{
    	    client.Headers[HttpRequestHeader.ContentType] = "application/json";
    	    client.Encoding = Encoding.UTF8;
    	    result = client.UploadString(postUrl, "POST", postData);
    	}
    	//Get 
    	var wc = new WebClient();
    	var jsonResult = wc.DownloadString(Url);
    

    封装好Http的方法,直接传入Url和postData即可,不必每次创建request对象。

    更新:the remote server returned an error (401) unauthorized

        var req = (HttpWebRequest)WebRequest.Create(tgtUrl);
        req.UseDefaultCredentials = true;
        req.PreAuthenticate = true;
        req.Credentials = CredentialCache.DefaultCredentials;
        var response = req.GetResponse();
        var stream = req.GetRequestStream();
    
        var wc = new WebClient();
        //Set default credential, avoid unauthorize error
        wc.Credentials = CredentialCache.DefaultCredentials;
        wc.UseDefaultCredentials = true;
  • 相关阅读:
    更改PHP配置文件php.ini解决xmlhttp的utf8乱码
    php 单引号 双引号 区别
    zend soap looks like we got no XML document
    使用UltraEdit32编辑器格式化源码功能 XML、Java、C/C++、C#
    php str_replace 单引号 双引号 区别
    PHP将XML文件转换成PHP数组
    简述PHP4和PHP5版本下解析XML文档的操作方法
    HowTo: Fix SimpleXML CDATA problem in php
    XML中 CDATA and PCDATA 的区别
    php 生成xml 的四种方式
  • 原文地址:https://www.cnblogs.com/shy-huang/p/7345285.html
Copyright © 2011-2022 走看看