• 创建HttpRequest传输数据


    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Net;

    namespace Bookmark.HttpAPI
    {
        
    public class WebRequestAPI
        {
            
    private static CookieContainer cookie=new CookieContainer ();


            
    #region 同步通过POST方式发送数据
            
    /// <summary>
            
    /// 通过POST方式发送数据
            
    /// </summary>
            
    /// <param name="Url">url</param>
            
    /// <param name="postDataStr">Post数据</param>
            
    /// <param name="cookie">Cookie容器</param>
            
    /// <returns></returns>
            public static string SendDataByPost(string Url, string postDataStr)
            {
                HttpWebRequest request 
    = (HttpWebRequest)WebRequest.Create(Url);
                
    if (cookie.Count == 0)
                {
                    request.CookieContainer 
    = new CookieContainer();
                    cookie 
    = request.CookieContainer;
                }
                
    else
                {
                    request.CookieContainer 
    = cookie;
                }

                request.Method 
    = "POST";
                request.ContentType 
    = "application/x-www-form-urlencoded";
                request.ContentLength 
    = postDataStr.Length;
                Stream myRequestStream 
    = request.GetRequestStream();
                StreamWriter myStreamWriter 
    = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
                myStreamWriter.Write(postDataStr);
                myStreamWriter.Close();

                HttpWebResponse response 
    = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream 
    = response.GetResponseStream();
                StreamReader myStreamReader 
    = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                
    string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();

                
    return retString;
            }
            
    #endregion

            
    #region 同步通过GET方式发送数据
            
    /// <summary>
            
    /// 通过GET方式发送数据
            
    /// </summary>
            
    /// <param name="Url">url</param>
            
    /// <param name="postDataStr">GET数据</param>
            
    /// <param name="cookie">GET容器</param>
            
    /// <returns></returns>
            public static string SendDataByGET(string Url, string postDataStr)
            {
                HttpWebRequest request 
    = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?"+ postDataStr);
                
    if (cookie.Count == 0)
                {
                    request.CookieContainer 
    = new CookieContainer();
                    cookie 
    = request.CookieContainer;
                }
                
    else
                {
                    request.CookieContainer 
    = cookie;
                }

                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.GetEncoding("utf-8"));
                
    string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();

                
    return retString;
            }
            
    #endregion
        }
    }
  • 相关阅读:
    flume采集log4j日志到kafka
    解决spring 事务管理默认不支持SQLException等运行时异常
    基于黑名单的xss过滤器
    VMware创建Linux虚拟机并安装CentOS(三)
    dubbo 常见错误
    Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
    zookeeper工作原理、安装配置、工具命令简介
    VMware创建Linux虚拟机并安装CentOS(二)
    NSPredicate谓词
    IOS网络请求之NSURLSession使用详解
  • 原文地址:https://www.cnblogs.com/hubcarl/p/1732492.html
Copyright © 2020-2023  润新知