• HttpWebRequest


    using System;
    using System.Collections.Generic;
    using System.Text;

    using System.Net;
    using System.IO;

    namespace  MyWebRequest

    {
        internal class MobileUtils
        {
            const string sUserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            const string sContentType = "application/x-www-form-urlencoded";
            const string sRequestEncoding = "utf-8";
            const string sResponseEncoding = "utf-8";

            /// <summary>
            /// Post data到url
            /// </summary>
            /// <param name="data">要post的数据</param>
            /// <param name="url">目标url</param>
            /// <returns>服务器响应</returns>
            public static string PostDataToUrl(string data, string url)
            {
                Encoding encoding = Encoding.GetEncoding(sRequestEncoding);
                byte[] bytesToPost = encoding.GetBytes(data);
                return PostDataToUrl(bytesToPost, url);
            }

            /// <summary>
            /// Post data到url
            /// </summary>
            /// <param name="data">要post的数据</param>
            /// <param name="url">目标url</param>
            /// <returns>服务器响应</returns>
            public static string PostDataToUrl(byte[] data, string url)
            {
                #region 创建httpWebRequest对象
                WebRequest webRequest = WebRequest.Create(url);
                HttpWebRequest httpRequest = webRequest as HttpWebRequest;
                if (httpRequest == null)
                {
                    throw new ApplicationException(
                        string.Format("Invalid url string: {0}", url)
                        );
                }
                #endregion

                #region 填充httpWebRequest的基本信息
                httpRequest.UserAgent = sUserAgent;
                httpRequest.ContentType = sContentType;
                httpRequest.Method = "POST";
                #endregion

                #region 填充要post的内容
                httpRequest.ContentLength = data.Length;
                Stream requestStream = httpRequest.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
                #endregion

                #region 发送post请求到服务器并读取服务器返回信息
                Stream responseStream;
                try
                {
                    responseStream = httpRequest.GetResponse().GetResponseStream();
                }

                catch (Exception e)
                {
                    // log error WinForm调试方式
                    //Console.WriteLine(
                    //    string.Format("POST操作发生异常:{0}", e.Message)
                    //    );
                    throw e;
                }
                #endregion

                #region 读取服务器返回信息
                string stringResponse = string.Empty;
                using (StreamReader responseReader =
                    new StreamReader(responseStream, Encoding.GetEncoding(sResponseEncoding)))
                {
                    stringResponse = responseReader.ReadToEnd();
                }
                responseStream.Close();
                #endregion
                return stringResponse;
            }

            /// <summary>
            /// 将字符编码为Base64
            /// </summary>
            /// <param name="encodeType">编码方式</param>
            /// <param name="input">明文字符</param>
            /// <returns>字符串</returns>
            public static string EncodeBase64(string encodeType, string input)
            {
                string result = string.Empty;
                byte[] bytes = Encoding.GetEncoding(encodeType).GetBytes(input);
                try
                {
                    result = Convert.ToBase64String(bytes);
                }
                catch
                {
                    result = input;
                }
                return result;
            }
            /// <summary>
            /// 将字符编码为Base64
            /// </summary>
            /// <param name="encodeType">编码方式</param>
            /// <param name="input">明文字符</param>
            /// <returns>字符串</returns>
            public static string DecodeBase64(string encodeType, string input)
            {
                string decode = string.Empty;
                byte[] bytes = Convert.FromBase64String(input);
                try
                {
                    decode = Encoding.GetEncoding(encodeType).GetString(bytes);
                }
                catch
                {
                    decode = input;
                }
                return decode;
            }
        }
    }

    try
                {
                    string action = Request.QueryString["otype"];
                    if (!string.IsNullOrEmpty(action))
                    {

                        Stream qs = Request.InputStream;
                        System.Text.Encoding encoding = Request.ContentEncoding;//1
                        StreamReader reader = new StreamReader(qs, encoding);//2

                        ErrorLog.Write(Request.RawUrl);

                        //string contentStr = reader.ReadToEnd();
                        char[] data = new char[Request.ContentLength];
                        ErrorLog.Write("data.Length=" + data.Length);
                        reader.Read(data, 0, data.Length);

                        String contentStr = new string(data);

                        ErrorLog.Write(contentStr);

                        Context myContext = new Context(action);
                        string returnXml = myContext.ContextInterface(contentStr);

                        ErrorLog.Write("returnXml=" + returnXml);

                        Response.AddHeader("Content-Type""text/xml");

                        Response.Clear();
                        Response.Write(returnXml.ToString());
                        //Response.End();
                    }
                    else
                    {
                        Response.Write("error");
                    }
                }
                catch (Exception ex)
                {
                    ErrorLog.Write("ex:", ex);
                }
  • 相关阅读:
    Spring AOP
    TestNG配合ant脚本进行单元测试
    TestNG配合catubuter统计单元测试的代码覆盖率
    junit配合catubuter统计单元测试的代码覆盖率
    TestNG离线安装步骤
    spring 整合redis集群中使用@autowire无效问题的解决办法
    @Repository、@Service、@Controller 和 @Component
    用VMware克隆CentOS 6.5如何进行网络设置
    centos 6.5 dhcp桥接方式上网络设置
    centos 6.5 关闭防火墙
  • 原文地址:https://www.cnblogs.com/zhuawang/p/2069995.html
Copyright © 2020-2023  润新知