zoukankan      html  css  js  c++  java
  • .NET后台访问其他站点代码整理

    HttpWebRequest request = WebRequest.Create(@"http://localhost:8080/mail/SendMail") as HttpWebRequest;
    
            request.Method = "post";
    
            User user = (User)Session["CurrentUser"];
    
            // 拼接成请求参数串,并进行编码,成为字节  
            string postData = "send=" + send + "&copy=" + copy + "&secret=" + secret + "&subject=" + HttpUtility.UrlEncode(this.txtTopic.Text, Encoding.GetEncoding("GBK")) + "&username=" + user.Email + "&pass=" + user.MailPwd + "&content=" + HttpUtility.UrlEncode(this.ftbContent.Text, Encoding.GetEncoding("GBK"));
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] byte1 = encoding.GetBytes(postData);
            // 设置请求的参数形式  
            request.ContentType = "application/x-www-form-urlencoded";
            request.Headers.Add("content", "text/html; charset=gbk");
    
            // 设置请求参数的长度.  
            request.ContentLength = byte1.Length;
    
            // 取得发向服务器的流  
            System.IO.Stream newStream = request.GetRequestStream();
    
            // 使用 POST 方法请求的时候,实际的参数通过请求的 Body 部分以流的形式传送  
            newStream.Write(byte1, 0, byte1.Length);
    
            // 完成后,关闭请求流.  
            newStream.Close();
    
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
            // 然后可以得到以流的形式表示的回应内容  
            System.IO.Stream receiveStream
                = response.GetResponseStream();
    
            // 还可以将字节流包装为高级的字符流,以便于读取文本内容   
            // 需要注意编码  
            System.IO.StreamReader readStream
                = new System.IO.StreamReader(receiveStream, Encoding.GetEncoding("GB2312"));
    
            string res = readStream.ReadToEnd();
    
            // 完成后要关闭字符流,字符流底层的字节流将会自动关闭  
            response.Close();
            readStream.Close();
    HttpWebRequest req = HttpWebRequest.Create(@"http://localhost:8080/mail/getmail.jsp") as HttpWebRequest;
    
            req.Method = "post";
    
            User user = (User)Session["CurrentUser"];
            // 拼接成请求参数串,并进行编码,成为字节  
            string postData = "page=" + PageNor + "&username=" + user.Email + "&pass=" + user.MailPwd;
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] byte1 = encoding.GetBytes(postData);
            // 设置请求的参数形式  
            req.ContentType = "application/x-www-form-urlencoded";
    
            // 设置请求参数的长度.  
            req.ContentLength = byte1.Length;
    
            // 取得发向服务器的流  
            System.IO.Stream newStream = req.GetRequestStream();
    
            // 使用 POST 方法请求的时候,实际的参数通过请求的 Body 部分以流的形式传送  
            newStream.Write(byte1, 0, byte1.Length);
    
            // 完成后,关闭请求流.  
            newStream.Close();
    
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
    
            // 然后可以得到以流的形式表示的回应内容  
            System.IO.Stream receiveStream
                = response.GetResponseStream();
    
            // 还可以将字节流包装为高级的字符流,以便于读取文本内容   
            // 需要注意编码  
            System.IO.StreamReader readStream
                = new System.IO.StreamReader(receiveStream, Encoding.GetEncoding("GB2312"));
    
            string res = readStream.ReadToEnd();
    
            // 完成后要关闭字符流,字符流底层的字节流将会自动关闭  
            response.Close();
            readStream.Close();
    
            if (!res.Equals("0")) 
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("id");
                dt.Columns.Add("subject");
                dt.Columns.Add("sender");
                dt.Columns.Add("date");
                string[] ss = res.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string s in ss)
                {
                    DataRow dr = dt.NewRow();
                    string[] str = s.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
                    dr["id"] = str[0];
                    dr["subject"] = str[3];
                    dr["sender"] = str[1];
                    dr["date"] = DateTime.Parse(str[5]);
                    dt.Rows.Add(dr);
                }
    
                int totalCount = 0;
                User curUser = (User)Session["CurrentUser"];
                int isDelete = 0;
    
                DataTable dt1 = SendProcessManager.GetSendAffairsByProc(curUser.ID, isDelete, PageNor, CommonInfo.PageSize, ref totalCount);
                if (totalCount > 0 && dt != null && dt.Rows.Count == 0)
                {
                    PageNor--;
                    dt1 = SendProcessManager.GetSendAffairsByProc(curUser.ID, isDelete, PageNor, CommonInfo.PageSize, ref totalCount);
                }
    
                List<MessageModel> list = new List<MessageModel>();
    
                foreach (DataRow dr in dt.Rows) 
                {
                    MessageModel mm = new MessageModel();
                    mm.Id = dr["id"].ToString();
                    mm.Subject = dr["subject"].ToString();
                    mm.Sender = dr["sender"].ToString();
                    mm.Date = DateTime.Parse(dr["date"].ToString());
                    mm.Status = "2";
    
                    list.Add(mm);
                }
    
                foreach (DataRow dr in dt1.Rows) 
                {
                    MessageModel mm = new MessageModel();
                    mm.Id = dr["INFOID"].ToString();
                    mm.CurLevel = dr["CurLevel"].ToString();
                    mm.UesrID = dr["UserID"].ToString();
                    mm.Date = DateTime.Parse(dr["SENDTIME"].ToString());
                    mm.Subject = dr["Topic"].ToString();
                    mm.AnnexCount = dr["AnnexCount"].ToString();
                    mm.ReadStatus = dr["ReadStatus"].ToString();
                    mm.Position = dr["Position"].ToString();
                    mm.Sender = dr["UserName"].ToString();
                    mm.RelateUserID = dr["RelateUserID"].ToString();
                    mm.InfoTableType = dr["InfoTableType"].ToString();
                    mm.Level = dr["Level"].ToString();
                    mm.Status = "1";
    
                    list.Add(mm);
                }
    
                var m = from l in list orderby l.Date descending select l;
    
                this.gvAffairs.DataSource = m;
                this.gvAffairs.DataBind();
    HttpWebRequest request = WebRequest.Create(@"http://localhost:8080/mail/maildetail.jsp") as HttpWebRequest;
    
                request.Method = "post";
    
                // 拼接成请求参数串,并进行编码,成为字节  
                string postData = "id=" + this.hfAffairId.Value + "&username=" + this.username + "&pass=" + this.pass;
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] byte1 = encoding.GetBytes(postData);
                // 设置请求的参数形式  
                request.ContentType = "application/x-www-form-urlencoded";
    
                // 设置请求参数的长度.  
                request.ContentLength = byte1.Length;
    
                // 取得发向服务器的流  
                System.IO.Stream newStream = request.GetRequestStream();
    
                // 使用 POST 方法请求的时候,实际的参数通过请求的 Body 部分以流的形式传送  
                newStream.Write(byte1, 0, byte1.Length);
    
                // 完成后,关闭请求流.  
                newStream.Close();
    
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                // 然后可以得到以流的形式表示的回应内容  
                System.IO.Stream receiveStream
                    = response.GetResponseStream();
    
                // 还可以将字节流包装为高级的字符流,以便于读取文本内容   
                // 需要注意编码  
                System.IO.StreamReader readStream
                    = new System.IO.StreamReader(receiveStream, Encoding.GetEncoding("GB2312"));
    
                string res = readStream.ReadToEnd();
    
                // 完成后要关闭字符流,字符流底层的字节流将会自动关闭  
                response.Close();
                readStream.Close();
    
                string[] ss = res.Split(new string[] { "&#@" }, StringSplitOptions.RemoveEmptyEntries);
    
                this.hfAffairId.Value = ss[0];
                this.lblTopic.Text = ss[1];
                this.lblContents.Text = ss[4];
                this.lblCreateUser.Text = ss[2];
                this.lblBeginAffairTime.Text = DateTime.Parse(ss[3]).ToString("yyyy-MM-dd HH:mm:ss");
                this.flowTr.Style.Add("display", "none");
                this.sendTr.Style.Add("display", "");
                this.lblSend.Text = ((User)Session["CurrentUser"]).Email;
  • 相关阅读:
    oracle转义用单引号
    【转】plsql 永久注册码适用个版本
    winform datagridview某一列设为自动宽度
    Allow windows service to "Interact with desktop"
    Format a Hard Drive in Csharp C#格式化总结
    Lib New
    大嫂的HTML
    ASP.NET 分页控件
    linux搭建常用命令(运行jar,查看进程)
    如何用navicat连接linux服务器上的mysql以及重启服务
  • 原文地址:https://www.cnblogs.com/wpcnblog/p/3688586.html
Copyright © 2011-2022 走看看