XMLHTTP实现POST数据的源代码

Posted on 2005-09-08 15:33  齐国老兵  阅读(2557)  评论(3编辑  收藏  举报
  1using System;
  2using MSXML2;
  3using System.Web;
  4namespace JJBase.NET.HTTP
  5{
  6    /// <summary>
  7    /// JJBase 的摘要说明。
  8    /// </summary>
  9    /// RFC2616 - Hypertext Transfer Protocol -- HTTP/1.1
 10    /// 协议参考:http://www.cnpaf.net/Class/Rfcen/053291853591170.html
 11    /// author:梁俊杰
 12    /// date:2005-8-23
 13    /// 功能:实现http数据包的发送和接收

 14    public class msXML
 15    {
 16        
 17        public struct NetData
 18        {
 19            public RequestHeader RequestHead;
 20            public ResponseHeader ResponseHead;
 21        }

 22        public struct RequestHeader
 23        {
 24            public string Method;
 25            public string Url;
 26            public string User;
 27            public string Password;
 28            public string Accept;
 29            public string AcceptCharset;
 30            public string AcceptEncoding;
 31            public string AcceptLanguage;
 32            public string ContentType;
 33            public string Authorization;
 34            public string CacheControl; 
 35            public string Expect;
 36            public string From;
 37            public string Host;
 38            public string IfMatch;
 39            public string IfModifiedSince;
 40            public string IfNoneMatch;
 41            public string IfRange;
 42            public string IfUnmodifiedSince;
 43            public string MaxForwards;
 44            public string ProxyAuthorization;
 45            public string Range;
 46            public string Referer;
 47            public string TE;
 48            public string UserAgent;
 49            public string Pragma;
 50            public string Connection;
 51            public string ContentLength;
 52            public string Cookie;
 53            public string SendData;        
 54        }

 55        public  struct ResponseHeader
 56        {
 57            public string StatusCode;
 58            public string Allow;
 59            public DateTime Date;
 60            public string ContentEncoding;
 61            public string ContentLanguage;
 62            public string ContentLength;
 63            public string ContentLocation;
 64            public string ContentMD5;
 65            public string ContentRange;
 66            public string ContentType;
 67            public string LastModified;
 68            public string Expires;
 69            public string AcceptRanges;
 70            public string Age;
 71            public string ETag;
 72            public string Location;
 73            public string ProxyAuthenticate;
 74            public string RetryAfter;
 75            public string Server;
 76            public string Vary;
 77            public string WWWAuthenticate;
 78            public string Pragma;
 79            public string Connection;
 80            public string SetCookie;    
 81            public string GetData;
 82            public string AllHeaders;
 83        }

 84        
 85        public msXML()
 86        {
 87            //
 88            // TODO: 在此处添加构造函数逻辑
 89            //
 90        }

 91        public string GetHtmlData(ref NetData nd)
 92        {
 93            string result="";
 94            string User="";
 95            string Password="";
 96            string Method="";
 97            string g_AllResponseHeaders="";
 98            //处理提交数据前的数据头
 99            if (nd.RequestHead.User!=""&&nd.RequestHead.User!=null) User=nd.RequestHead.User;
100            if (nd.RequestHead.Password!=""&&nd.RequestHead.Password!=null) Password=nd.RequestHead.Password;
101            if (nd.RequestHead.Method.ToString()==""&&nd.RequestHead.Method==null{return "";}
102            else{
103            Method=nd.RequestHead.Method.ToString().ToUpper();
104            }

105            MSXML2.ServerXMLHTTP SXmlHttp = new MSXML2.ServerXMLHTTP();
106            if (User==""){SXmlHttp.open(Method,nd.RequestHead.Url,false,"","");}
107            else{
108                SXmlHttp.open(Method,nd.RequestHead.Url,false,User,Password);}

109            if (nd.RequestHead.Accept!=""&&nd.RequestHead.Accept!=null) SXmlHttp.setRequestHeader("Accept",nd.RequestHead.Accept);
110            if (nd.RequestHead.AcceptCharset!=""&&nd.RequestHead.AcceptCharset!=null) SXmlHttp.setRequestHeader("Accept-Charset",nd.RequestHead.AcceptCharset);
111            if (nd.RequestHead.AcceptEncoding!=""&&nd.RequestHead.AcceptEncoding!=null) SXmlHttp.setRequestHeader("Accept-Encoding",nd.RequestHead.AcceptEncoding);
112            if (nd.RequestHead.AcceptLanguage!=""&&nd.RequestHead.AcceptLanguage!=null) SXmlHttp.setRequestHeader("Accept-Language",nd.RequestHead.AcceptLanguage);
113            if (nd.RequestHead.ContentType!=""&&nd.RequestHead.ContentType!=null) SXmlHttp.setRequestHeader("Content-Type",nd.RequestHead.ContentType);
114            if (nd.RequestHead.Authorization!=""&&nd.RequestHead.Authorization!=null) SXmlHttp.setRequestHeader("Authorization",nd.RequestHead.Authorization);
115            if (nd.RequestHead.CacheControl!=""&&nd.RequestHead.CacheControl!=null) SXmlHttp.setRequestHeader("Cache-Control",nd.RequestHead.CacheControl);
116            if (nd.RequestHead.Expect!=""&&nd.RequestHead.Expect!=null) SXmlHttp.setRequestHeader("Expect",nd.RequestHead.Expect);
117            if (nd.RequestHead.From!=""&&nd.RequestHead.From!=null) SXmlHttp.setRequestHeader("From",nd.RequestHead.From);
118            if (nd.RequestHead.Host!=""&&nd.RequestHead.Host!=null) SXmlHttp.setRequestHeader("Host",nd.RequestHead.Host);
119            if (nd.RequestHead.IfMatch!=""&&nd.RequestHead.IfMatch!=null) SXmlHttp.setRequestHeader("If-Match",nd.RequestHead.IfMatch);
120            if (nd.RequestHead.IfModifiedSince!=""&&nd.RequestHead.IfModifiedSince!=null) SXmlHttp.setRequestHeader("If-Modified-Since",nd.RequestHead.IfModifiedSince);
121            if (nd.RequestHead.IfNoneMatch!=""&&nd.RequestHead.IfNoneMatch!=null) SXmlHttp.setRequestHeader("If-None-Match",nd.RequestHead.IfNoneMatch);
122            if (nd.RequestHead.IfRange!=""&&nd.RequestHead.IfRange!=null) SXmlHttp.setRequestHeader("If-Range",nd.RequestHead.IfRange);
123            if (nd.RequestHead.IfUnmodifiedSince!=""&&nd.RequestHead.IfUnmodifiedSince!=null) SXmlHttp.setRequestHeader("If-Unmodified-Since",nd.RequestHead.IfUnmodifiedSince);
124            if (nd.RequestHead.MaxForwards!=""&&nd.RequestHead.MaxForwards!=null) SXmlHttp.setRequestHeader("Max-Forwards",nd.RequestHead.MaxForwards);
125            if (nd.RequestHead.ProxyAuthorization!=""&&nd.RequestHead.ProxyAuthorization!=null) SXmlHttp.setRequestHeader("Proxy-Authorization",nd.RequestHead.ProxyAuthorization);
126            if (nd.RequestHead.Range!=""&&nd.RequestHead.Range!=null) SXmlHttp.setRequestHeader("Range",nd.RequestHead.Range);
127            if (nd.RequestHead.Referer!=""&&nd.RequestHead.Referer!=null) SXmlHttp.setRequestHeader("Referer",nd.RequestHead.Referer);
128            if (nd.RequestHead.TE!=""&&nd.RequestHead.TE!=null) SXmlHttp.setRequestHeader("TE",nd.RequestHead.TE);
129            if (nd.RequestHead.UserAgent!=""&&nd.RequestHead.UserAgent!=null) SXmlHttp.setRequestHeader("User-Agent",nd.RequestHead.UserAgent);
130            if (nd.RequestHead.Pragma!=""&&nd.RequestHead.Pragma!=null) SXmlHttp.setRequestHeader("Pragma",nd.RequestHead.Pragma);
131            if (nd.RequestHead.Connection!=""&&nd.RequestHead.Connection!=null) SXmlHttp.setRequestHeader("Connection",nd.RequestHead.Connection);
132            if (nd.RequestHead.ContentLength!=""&&nd.RequestHead.ContentLength!=null) SXmlHttp.setRequestHeader("Content-Length",nd.RequestHead.ContentLength);
133            if (nd.RequestHead.Cookie!=""&&nd.RequestHead.Cookie!=null) SXmlHttp.setRequestHeader("Cookie",nd.RequestHead.Cookie);
134
135            switch(Method)
136            {
137                case "GET":
138                {
139                    try
140                    {
141                        SXmlHttp.send("");
142                    }

143                    catch(Exception ex)
144                    {
145                        return ex.Message.ToString();
146                    }

147                    finally{}
148                    break;
149                }

150                case "POST":
151                {
152                    try
153                    {
154                        SXmlHttp.setRequestHeader("Content-Length",nd.RequestHead.SendData.Length.ToString ());
155                        SXmlHttp.send(nd.RequestHead.SendData);
156                    }

157                    catch(Exception ex)
158                    {
159                        return ex.Message.ToString();
160                    }

161                    finally{}
162                    break;
163                }

164                
165            }

166            nd.ResponseHead.StatusCode=SXmlHttp.status.ToString();
167            g_AllResponseHeaders = SXmlHttp.getAllResponseHeaders();
168            nd.ResponseHead.AllHeaders=g_AllResponseHeaders;
169            //处理返回的数据头
170            if (g_AllResponseHeaders.IndexOf("Allow")>0) nd.ResponseHead.Allow=SXmlHttp.getResponseHeader("Allow");
171            if (g_AllResponseHeaders.IndexOf("Date")>0) nd.ResponseHead.Date=Convert.ToDateTime(SXmlHttp.getResponseHeader("Date"));
172            if (g_AllResponseHeaders.IndexOf("Content-Encoding")>0) nd.ResponseHead.ContentEncoding=SXmlHttp.getResponseHeader("Content-Encoding");
173            if (g_AllResponseHeaders.IndexOf("Content-Language")>0) nd.ResponseHead.ContentLanguage=SXmlHttp.getResponseHeader("Content-Language");
174            if (g_AllResponseHeaders.IndexOf("Content-Length")>0) nd.ResponseHead.ContentLength=SXmlHttp.getResponseHeader("Content-Length");
175            if (g_AllResponseHeaders.IndexOf("Content-Location")>0) nd.ResponseHead.ContentLocation=SXmlHttp.getResponseHeader("Content-Location");
176            if (g_AllResponseHeaders.IndexOf("Content-MD5")>0) nd.ResponseHead.ContentMD5=SXmlHttp.getResponseHeader("Content-MD5");
177            if (g_AllResponseHeaders.IndexOf("Content-Range")>0) nd.ResponseHead.ContentRange=SXmlHttp.getResponseHeader("Content-Range");
178            if (g_AllResponseHeaders.IndexOf("Content-Type")>0) nd.ResponseHead.ContentType=SXmlHttp.getResponseHeader("Content-Type");
179            if (g_AllResponseHeaders.IndexOf("Last-Modified")>0) nd.ResponseHead.LastModified=SXmlHttp.getResponseHeader("Last-Modified");
180            if (g_AllResponseHeaders.IndexOf("Expires")>0) nd.ResponseHead.Expires=SXmlHttp.getResponseHeader("Expires");
181            if (g_AllResponseHeaders.IndexOf("Accept-Ranges")>0) nd.ResponseHead.AcceptRanges=SXmlHttp.getResponseHeader("Accept-Ranges");
182            if (g_AllResponseHeaders.IndexOf("Age")>0) nd.ResponseHead.Age=SXmlHttp.getResponseHeader("Age");
183            if (g_AllResponseHeaders.IndexOf("ETag")>0) nd.ResponseHead.ETag=SXmlHttp.getResponseHeader("ETag");
184            if (g_AllResponseHeaders.IndexOf("Location")>0) nd.ResponseHead.Location=SXmlHttp.getResponseHeader("Location");
185            if (g_AllResponseHeaders.IndexOf("Proxy-Authenticate")>0) nd.ResponseHead.ProxyAuthenticate=SXmlHttp.getResponseHeader("Proxy-Authenticate");
186            if (g_AllResponseHeaders.IndexOf("Retry-After")>0) nd.ResponseHead.RetryAfter=SXmlHttp.getResponseHeader("Retry-After");
187            if (g_AllResponseHeaders.IndexOf("Server")>0) nd.ResponseHead.Server=SXmlHttp.getResponseHeader("Server");
188            if (g_AllResponseHeaders.IndexOf("Vary")>0) nd.ResponseHead.Vary=SXmlHttp.getResponseHeader("Vary");
189            if (g_AllResponseHeaders.IndexOf("WWW-Authenticate")>0) nd.ResponseHead.WWWAuthenticate=SXmlHttp.getResponseHeader("WWW-Authenticate");
190            if (g_AllResponseHeaders.IndexOf("Pragma")>0) nd.ResponseHead.RetryAfter=SXmlHttp.getResponseHeader("Retry-After");
191            if (g_AllResponseHeaders.IndexOf("Connection")>0) nd.ResponseHead.Connection=SXmlHttp.getResponseHeader("Connection");
192            if (g_AllResponseHeaders.IndexOf("Set-Cookie")>0) nd.ResponseHead.SetCookie=SXmlHttp.getResponseHeader("Set-Cookie");
193            if (g_AllResponseHeaders.IndexOf("Retry-After")>0) nd.ResponseHead.RetryAfter=SXmlHttp.getResponseHeader("Retry-After");
194            if (g_AllResponseHeaders.IndexOf("Retry-After")>0) nd.ResponseHead.RetryAfter=SXmlHttp.getResponseHeader("Retry-After");
195            //取得字符集,不进行此项操作可能出现乱码
196            string charset="gb2312";
197            if (nd.ResponseHead.ContentType.ToString().IndexOf("utf-8")>0){charset="utf-8";}
198            result=BYTE2STR((byte[])SXmlHttp.responseBody,charset);
199            nd.ResponseHead.GetData=result;
200            SXmlHttp=null;
201            return result;
202        }

203        private string BYTE2STR(byte[] vIn,string charset)
204        {
205//            int ThisCharCode,NextCharCode,HZCode;
206//            string strReturn="";
207//            for (int i = 1; i <= vIn.Length ; i++) 
208//            { 
209//                ThisCharCode =Convert.ToInt32(vIn[i]); 
210//                if (ThisCharCode < 128) 
211//                { 
212//                    strReturn = strReturn +Convert.ToChar(ThisCharCode); 
213//                } 
214//                else 
215//                { 
216//                    NextCharCode =vIn[i+1]; 
217//                    HZCode=ThisCharCode * 256 +NextCharCode;
218//                    strReturn = strReturn + Convert.ToChar(HZCode); 
219//                    i = i + 1; 
220//                } 
221//            } 
222//            return strReturn;
223            try
224            {        
225                return System.Text.Encoding.GetEncoding(charset).GetString(vIn).ToString();
226            }

227            catch(Exception ex){return ex.Message;}
228        }

229        
230    }

231}

Copyright © 2024 齐国老兵
Powered by .NET 9.0 on Kubernetes