分类

链接

2016 年 8 月
1234567
891011121314
15161718192021
22232425262728
293031  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > .NET, MQ > 正文
共享办公室出租
C#调用安全验证下的ActiveMQ REST管理接口
.NET, MQ 暂无评论 阅读(1,438)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Net;
  9. using System.IO;
  10.  
  11. namespace WindowsFormsApplication1
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             String url = textBox2.Text;
  23.             String admin = url.Substring(0, url.IndexOf("hawtio")) + "admin/";
  24.  
  25.             HttpWebRequest request = WebRequest.Create(admin) as HttpWebRequest;
  26.             request.CookieContainer = new CookieContainer();
  27.             request.KeepAlive = false;
  28.             request.AllowAutoRedirect = false;
  29.             request.PreAuthenticate = true;
  30.             request.Credentials = new System.Net.NetworkCredential("admin", "admin"); 
  31.  
  32.             WebResponse response = null;
  33.             try
  34.             {
  35.                 response = request.GetResponse();
  36.             }
  37.             catch (Exception ex)
  38.             {
  39.                 textBox1.Text = ex.Message;
  40.             }
  41.  
  42.             if (response != null)
  43.             {
  44.                 Stream s = response.GetResponseStream();
  45.                 StreamReader sr = new StreamReader(s, Encoding.Default, true);
  46.                 string html = sr.ReadToEnd();
  47.                 Encoding encoding = sr.CurrentEncoding;
  48.                 sr.Close();
  49.                 s.Close();
  50.                 response.Close();
  51.  
  52.                 HttpWebRequest request1 = WebRequest.Create(url) as HttpWebRequest;
  53.                 request1.KeepAlive = false;
  54.                 request1.AllowAutoRedirect = false;
  55.                 request1.PreAuthenticate = true;
  56.                 request1.Credentials = request.Credentials; 
  57.                 request1.CookieContainer = request.CookieContainer;
  58.                 request1.Headers.Set("Authorization", request.Headers.Get("Authorization"));
  59.                  
  60.  
  61.                 response = request1.GetResponse();
  62.                 if (response != null)
  63.                 {
  64.  
  65.                     s = response.GetResponseStream();
  66.                     sr = new StreamReader(s, Encoding.Default, true);
  67.                     string json = sr.ReadToEnd();
  68.                     sr.Close();
  69.                     s.Close();
  70.                     response.Close();
  71.  
  72.                     textBox1.Text = json;
  73.                 }
  74.  
  75.                  
  76.             }
  77.         }
  78.     }
  79. }

 

2. [图片] 1.jpg

3. [代码][C#]代码    

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Net;
  9. using System.IO;
  10.  
  11. namespace WindowsFormsApplication1
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             String url = textBox2.Text;
  23.             String admin = url.Substring(0, url.IndexOf("hawtio")) + "admin/";
  24.  
  25.             HttpWebRequest request = WebRequest.Create(admin) as HttpWebRequest;
  26.             request.CookieContainer = new CookieContainer();
  27.             request.KeepAlive = false;
  28.             request.AllowAutoRedirect = false;
  29.             request.PreAuthenticate = true;
  30.             request.Credentials = new System.Net.NetworkCredential("admin", "admin");
  31.  
  32.             WebResponse response = null;
  33.             try
  34.             {
  35.                 response = request.GetResponse();
  36.             }
  37.             catch (Exception ex)
  38.             {
  39.                 textBox1.Text = ex.Message;
  40.             }
  41.  
  42.             if (response != null)
  43.             {
  44.                 Stream s = response.GetResponseStream();
  45.                 StreamReader sr = new StreamReader(s, Encoding.Default, true);
  46.                 string html = sr.ReadToEnd();
  47.                 Encoding encoding = sr.CurrentEncoding;
  48.                 sr.Close();
  49.                 s.Close();
  50.                 response.Close();
  51.  
  52.                 HttpWebRequest request1 = WebRequest.Create(url) as HttpWebRequest;
  53.                 request1.KeepAlive = false;
  54.                 request1.AllowAutoRedirect = false;
  55.                 request1.PreAuthenticate = true;
  56.                 request1.Credentials = request.Credentials;
  57.                 request1.CookieContainer = request.CookieContainer;
  58.                 request1.Headers.Set("Authorization", request.Headers.Get("Authorization"));
  59.                 
  60.  
  61.                 response = request1.GetResponse();
  62.                 if (response != null)
  63.                 {
  64.  
  65.                     s = response.GetResponseStream();
  66.                     sr = new StreamReader(s, Encoding.Default, true);
  67.                     string json = sr.ReadToEnd();
  68.                     sr.Close();
  69.                     s.Close();
  70.                     response.Close();
  71.  
  72.                     textBox1.Text = json;
  73.                 } 
  74.             }
  75.         }
  76.     }
  77. }

4. [图片] 1.jpg

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:C#调用安全验证下的ActiveMQ REST管理接口 | Bruce's Blog

发表评论

留言无头像?