记Outlook插件与Web页面交互的各种坑 (含c# HttpWebRequest 连接https 的完美解决方法)
1) 方案一, 使用Web Service 基础功能没问题, 只是在连接https (ssh) 网站时, 需要针对https进行开发 (即http 和https 生成两套接口, 不太容易统一 ). 后来改为使用web页面交互(实质是.ashx) 结果也遇到了不少问题.
public class WebApiClient
{
public string Url { get; set; }
private CookieContainer Cookies = new CookieContainer(); public static string Get( string url )
{
//this code can fix https after .net 4.6
if (url.StartsWith("https", StringComparison.CurrentCultureIgnoreCase))
{
System.Net.ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
}
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded"; System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s = response.GetResponseStream();
using (StreamReader reader = new StreamReader(s))
{
string strValue = reader.ReadToEnd();
return strValue;
}
}
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
public Result<DataTable> CallWebApi(string action, string json )
{
return CallWebApi(action, json, null, null);
}
public Result<DataTable> CallWebApi(string action, string json, string fileName, byte[] fileData)
{
string responseContent;
var memStream = new MemoryStream();
var webRequest = (HttpWebRequest)WebRequest.Create(Url);
var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
//var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");
webRequest.Method = "POST";
//webRequest.Timeout = timeOut;
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
if (!string.IsNullOrEmpty(fileName))
{
const string filePartHeader =
"Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
"Content-Type: application/octet-stream\r\n\r\n";
var header = string.Format(filePartHeader, "file1", fileName);
var headerbytes = Encoding.UTF8.GetBytes(header);
memStream.Write(beginBoundary, , beginBoundary.Length);
memStream.Write(headerbytes, , headerbytes.Length);
memStream.Write(fileData, , fileData.Length);
} var stringKeyHeader = "\r\n--" + boundary +
"\r\nContent-Disposition: form-data; name=\"{0}\"" +
"\r\n\r\n{1}\r\n";
var bytes = Encoding.UTF8.GetBytes(string.Format(stringKeyHeader, "action", action));
memStream.Write(bytes, , bytes.Length);
bytes = Encoding.UTF8.GetBytes(string.Format(stringKeyHeader, "json", json));
memStream.Write(bytes, , bytes.Length); memStream.Write(endBoundary, , endBoundary.Length);
webRequest.ContentLength = memStream.Length;
var requestStream = webRequest.GetRequestStream();
memStream.Position = ;
var tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, , tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, , tempBuffer.Length);
requestStream.Close();
var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(),
Encoding.GetEncoding("utf-8")))
{
responseContent = httpStreamReader.ReadToEnd();
}
httpWebResponse.Close();
webRequest.Abort();
try
{
return JsonConvert.DeserializeObject<Result<DataTable>>(responseContent);
}
catch (Exception ex)
{
throw new ApplicationException("Parse server result error: " + responseContent, ex);
}
} }
if(url.StartsWith("https",StringComparison.CurrentCultureIgnoreCase)){System.Net.ServicePointManager.ServerCertificateValidationCallback=newRemoteCertificateValidationCallback(CheckValidationResult);}
privatestatic bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain,SslPolicyErrors errors){returntrue;}
ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls|SecurityProtocolType.Tls11|SecurityProtocolType.Tls12|SecurityProtocolType.Ssl3;
|
WebBrowser 其实是对 ActiveX 控件 SHDocVw 的封装,而这个SHDocVw的很多底层调用WebBrowser控件并没有提供实现,我们需要直接操作 SHDoceVw 控件来实现这些高级调用。操作方法如下: 2、在 Form1_Load 中添加如下语句 SHDocVw.WebBrowser wb = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance; 3、添加如下成员函数 private void WebBrowser_BeforeNavigate2(object pDisp, ref object URL, ref object Flags, 完成上述3步后,你post 数据时, 就会响应 BeforeNavigate2 事件,postDataText 中就是你post的数据。你也可以修改PostData,对这些数据进行转换或加密。 |
感觉会比较麻烦, 其实还有一种方法,就是利用WebBrowser的DocumentText属性注入脚本(执行脚本), 然后利用Ajax的方式和web服务器进行交互. 之后利用JS和C#互操作来完成相应的功能.
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
webBrowser1.ObjectForScripting=this;
webBrowser1.Document.InvokeScript("jsFunction",newstring[]{‘ssss’});
window.external.CSharpFunction(‘呵呵’);
记Outlook插件与Web页面交互的各种坑 (含c# HttpWebRequest 连接https 的完美解决方法)的更多相关文章
- (基础篇)PHP与Web页面交互
PHP与Web页面交互是实现PHP网站与用户交互的重要手段.在PHP中提供了两种与Web页面交互的方法,一种是通过Web表单提交数据,另一种是通过URL参数传递. 这里我们将详细讲解表单的相关知识,为 ...
- PHP与web 页面交互
PHP与Web页面交互是实现PHP网站与用户交互的重要手段.在PHP中提供了两种与Web页面交互的方法,一种是通过Web表单提交数据,另一种是通过URL参数传递. 这里我们将详细讲解表单的相关知识,为 ...
- 5.PHP与Web页面交互
PHP与Web页面交互 PHP中提供了两种与Web页面交互的方法,一种是通过Web表单提交数据,另一种是通过URL参数传递. 表单提交用户名字和密码: <form name "form ...
- Chrome插件触发web页面的事件
Chrome插件中不能直接调用Web页面的元素js,原因是chrome插件的机制http://stackoverflow.com/questions/17819344/triggering-a-cli ...
- php与web页面交互(二)
一.获取表单数据 1.1 使用POST()方法提交表单 ---POST()方法可以没有限制地传递数据到服务器,所提交的数据在后台传输,用户在浏览器端是看不到这一过程的,安全性高,适用于发送保密数据和 ...
- php与web页面交互
一.web表单 web表单的功能是让浏览者和网站有一个互动的平台.web表单主要用来在网页中发送数据到服务器. 1.1 表单的创建 使用form标记,并在其中插入相关的表单元素,即可创建一个表单. & ...
- web页面动态加载UserControl,并调用用户控件中的方法来初始化控件
1,HTML页 头部注册: <%@ Register Src="~/WorkLog/WorkLogNewV1/UserControl/CeShiBu.ascx" TagPre ...
- JAVA and JAVA WEB with TOMCAT and ECLIPSE 学习过程中遇到的字符乱码问题及解决方法汇总(随时补充)
JAVA语言具有跨平台,unicode字符集编码的特点. 但是在开发过程中处理数据时涉及到的字符编码问题零零散散,尤其是处理中文字符时一不留神就可能出现一堆奇奇怪怪的符号,俗称乱码. 对于乱码,究其原 ...
- Firefox火狐Flash插件卡死问题完美解决方法(转载)
http://www.ihacksoft.com/firefox-flash-protectedmode.html 其实这个问题以前就出现过,而最近该问题又出现在最新的 Windows 8.1 系统中 ...
随机推荐
- 涉及spring的相关概念
1.pojo 2.为了降低java开发的复杂性,spring采用了4中策略 (1).基于POJO的轻量级和最小侵入性编程 (2).通过依赖注入和接口编程实现松耦合 (3).基于切面和惯例进行声明式编程 ...
- 解决OV系列摄像头寄存器读数据无法收到的问题
最近工作中接了一款OV7725的sensor,由于平台已经接过很多的家的sensor也就没有太当回事.问题出现的很奇怪,再看了 register map后基本确定了要尽心register R/W测试 ...
- vim-addon-manager【转】
Vim是一个非常优秀的编辑器,但是没装插件的Vim就始终只是个编辑器而已,是插件让Vim变得更加强大. 但是插件装得多了,管理就成了问题,Vim本身并没有提供插件管理功能,往往时间一长,.vim/vi ...
- Linux学习目录
一.CentOS for Linux 大神博客:骏马金龙 Linux也可以参考,这篇博客进行学习 http://www.cnblogs.com/f-ck-need-u/p/7048359.html ...
- Gson和Fastjson的使用
转载自:JSON技术的调研报告 一.Google的Gson包的使用简单介绍. Gson类:解析json的最基础的工具类 JsonParser类:解析器来解析JSON到JsonElements的解析树 ...
- java用iText导出word文档
1.需要导入的jar包 2.导出word并下载其实是分两步的. 第一步是将需要导出的数据导出(上传)到服务器上 第二步是将服务器上的文档下载到本地 3. 第一步.上传文档 (1)设置响应信息以及构造上 ...
- hibernate createQuery查询传递参数的两种方式
String hql = "from InventoryTask it where it.orgId=:orgId"; Session session = getSession() ...
- Linux 进程间通信(一)(经典IPC:消息队列、信号量、共享存储)
有3种称作XSI IPC的IPC:消息队列.信号量.共享存储.这种类型的IPC有如下共同的特性. 每个内核中的IPC都用一个非负整数标志.标识符是IPC对象的内部名称,为了使多个合作进程能够在同一IP ...
- hive 安装警告 WARN conf.HiveConf: HiveConf of name hive.metastore.local does not exist
解决方法: 在0.10 0.11或者之后的HIVE版本 hive.metastore.local 属性不再使用. 在配置文件里面: <property> <name>hi ...
- 解决\build\outputs\apk\dream-debug.apk does not exist on disk错误
\build\outputs\apk\dream-debug.apk does not exist on disk.错误,apk一直装不到手机里. 最有效的解决方法:Build>Buid APK