<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Li Guoliang &#187; URLRequest</title>
	<atom:link href="http://liguoliang.com/tag/urlrequest/feed/" rel="self" type="application/rss+xml" />
	<link>http://liguoliang.com</link>
	<description>ActionScript Flex Java JEE PHP...</description>
	<lastBuildDate>Mon, 21 May 2012 17:04:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Flex中使用NavigateToURL实现HTML预览</title>
		<link>http://liguoliang.com/2010/use-navigatetourl-and-post-to-priview-html/</link>
		<comments>http://liguoliang.com/2010/use-navigatetourl-and-post-to-priview-html/#comments</comments>
		<pubDate>Wed, 10 Nov 2010 13:04:42 +0000</pubDate>
		<dc:creator>Guoliang</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[navigateToURL]]></category>
		<category><![CDATA[POST]]></category>
		<category><![CDATA[URLRequest]]></category>

		<guid isPermaLink="false">http://liguoliang.com/2010/use-navigatetourl-and-post-to-priview-html/</guid>
		<description><![CDATA[<p>尚未发现Flex中有可以直接渲染HTML代码的组件, 但可通过服务器端配合实现预览.</p>
<p>原理: Flex(尤指AIR环境), 使用URLLoader发送POST请求, 将要预览的HTML代码发送到服务器端Servlet, 服务器端收到请求后创建临时文件, 将HTML存到临时文件中. Flex端收到URLLoader Complete事件后, 使用navigateToURL navigate到Servlet上(使用GET), Servelet 返回文件内容 &#8212;- 在上述过程中, Flex端生成UUID作为代码读写的key.</p>
<p> </p><p class='read-more'><a href='http://liguoliang.com/2010/use-navigatetourl-and-post-to-priview-html/'>More...</a></p><p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></description>
			<content:encoded><![CDATA[<p>尚未发现Flex中有可以直接渲染HTML代码的组件, 但可通过服务器端配合实现预览.</p>
<p>原理: Flex(尤指AIR环境), 使用URLLoader发送POST请求, 将要预览的HTML代码发送到服务器端Servlet, 服务器端收到请求后创建临时文件, 将HTML存到临时文件中. Flex端收到URLLoader Complete事件后, 使用navigateToURL navigate到Servlet上(使用GET), Servelet 返回文件内容 &#8212;- 在上述过程中, Flex端生成UUID作为代码读写的key.</p>
<p> <span id="more-1733"></span>
</p>
<p>Flex端代码:</p>
<pre class="java" name="code">	protected var uuidPriview:String;
	/**
	 * 在浏览器中预览邮件, 解决方法: 生成uuid, 连同邮件内容发送到服务器端, 服务器端创建文件, 完毕后携带uuid Navigat到服务器端, 服务器端返回邮件内容, 实现预览.
	 * 在AIR中, 会将POST视为GET:
	 * in Adobe AIR, when using the navigateToURL() function, the runtime treats a URLRequest that uses the POST method (one that has its method property set to URLRequestMethod.POST) as using the GET method.
	 * @see http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/package.html#navigateToURL()
	 **/
	protected function priviewInBroswer():void {
		var urlReq:URLRequest = new URLRequest(PRIVIEW_URL);
		urlReq.method = URLRequestMethod.POST;
		var data:URLVariables = new URLVariables;
		data.content = &quot;html codes....&quot;; // 放入要预览的代码
		data.uuid = uuidPriview = UIDUtil.createUID(); // 生成UUID, 作为Java端文件名.
		urlReq.data = data;

		var urlLoader:URLLoader = new URLLoader();
		urlLoader.addEventListener(Event.COMPLETE, onPriviewSendSuccess);
		urlLoader.load(urlReq);
	}

	/** 预览请求发送结束后响应. */
	protected function onPriviewSendSuccess(e:Event):void {
		var urlReq:URLRequest = new URLRequest(PRIVIEW_URL);
		var data:URLVariables = new URLVariables;
		data.uuid = uuidPriview; // Get中携带uuid.
		urlReq.data = data;
		navigateToURL(urlReq); // 打开浏览器
	}</pre>
<p>Java端代码:</p>
<pre class="java" name="code">	/**
	 * 接收到Get请求后, 读取uuid, 读取uuid对应文件的内容, 写到response中, 客户端实现预览.
	 */
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String uuid = req.getParameter(&quot;uuid&quot;);
		String content = FileUtil.getStringFromFile(&quot;E:\\temp\\priview\\&quot; + uuid); // 读取内容

		resp.getWriter().write(content);
	}

	@Override
	/**
	 * 接受请求, 创建文件, 文件名称为创建的uuid.
	 */
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String content = req.getParameter(&quot;content&quot;);
		String uuid = req.getParameter(&quot;uuid&quot;);

		String filePath = &quot;E:\\temp\\priview\\&quot; + uuid;
		File pf = new File(filePath);
		pf.createNewFile();
		FileUtil.saveStringToFile(content, pf, &quot;utf-8&quot;); // 创建并写入文件

		resp.getWriter().write(&quot;get it&quot;);
	}</pre>
<p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></content:encoded>
			<wfw:commentRss>http://liguoliang.com/2010/use-navigatetourl-and-post-to-priview-html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flex: 使用URLRequest下载文件时的权限验证</title>
		<link>http://liguoliang.com/2010/flex-%e4%bd%bf%e7%94%a8urlrequest%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6%e6%97%b6%e7%9a%84%e6%9d%83%e9%99%90%e9%aa%8c%e8%af%81/</link>
		<comments>http://liguoliang.com/2010/flex-%e4%bd%bf%e7%94%a8urlrequest%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6%e6%97%b6%e7%9a%84%e6%9d%83%e9%99%90%e9%aa%8c%e8%af%81/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 15:49:08 +0000</pubDate>
		<dc:creator>Guoliang</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[FileReference]]></category>
		<category><![CDATA[URLRequest]]></category>

		<guid isPermaLink="false">http://liguoliang.com/2010/01/1132/</guid>
		<description><![CDATA[权限验证:
var fileRef:FileReference = new FileReference();
		var request:URLRequest = new URLRequest(serviceUrl);

		request.method = URLRequestMethod.POS<p class='read-more'><a href='http://liguoliang.com/2010/flex-%e4%bd%bf%e7%94%a8urlrequest%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6%e6%97%b6%e7%9a%84%e6%9d%83%e9%99%90%e9%aa%8c%e8%af%81/'>More...</a></p><p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></description>
			<content:encoded><![CDATA[<p>权限验证:</p>
<pre class="java" name="code">var fileRef:FileReference = new FileReference();
		var request:URLRequest = new URLRequest(serviceUrl);

		request.method = URLRequestMethod.POST;
		var encoder : Base64Encoder = new Base64Encoder();
		encoder.encode(userName + &quot;:&quot; + password);
		request.requestHeaders.push(new URLRequestHeader(&quot;Authorization&quot;, &quot;Basic &quot; + encoder.toString())); // ** 增加认证信息

		fileRef.addEventListener(Event.OPEN, onFileDownloadBegin);
		fileRef.addEventListener(Event.COMPLETE, onFileDownloadComplete);
		fileRef.addEventListener(IOErrorEvent.IO_ERROR, onFileDownloadError);
		fileRef.addEventListener(Event.CANCEL, onFileDownLoadCancel);
		fileRef.download(request, getExportFileName(serviceUrl));</pre>
<p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></content:encoded>
			<wfw:commentRss>http://liguoliang.com/2010/flex-%e4%bd%bf%e7%94%a8urlrequest%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6%e6%97%b6%e7%9a%84%e6%9d%83%e9%99%90%e9%aa%8c%e8%af%81/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flex: 使用URLRequest进行下载, 并监听有关事件</title>
		<link>http://liguoliang.com/2010/flex-%e4%bd%bf%e7%94%a8urlrequest%e8%bf%9b%e8%a1%8c%e4%b8%8b%e8%bd%bd-%e5%b9%b6%e7%9b%91%e5%90%ac%e6%9c%89%e5%85%b3%e4%ba%8b%e4%bb%b6/</link>
		<comments>http://liguoliang.com/2010/flex-%e4%bd%bf%e7%94%a8urlrequest%e8%bf%9b%e8%a1%8c%e4%b8%8b%e8%bd%bd-%e5%b9%b6%e7%9b%91%e5%90%ac%e6%9c%89%e5%85%b3%e4%ba%8b%e4%bb%b6/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 15:46:47 +0000</pubDate>
		<dc:creator>Guoliang</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[DownLoad]]></category>
		<category><![CDATA[URLRequest]]></category>

		<guid isPermaLink="false">http://liguoliang.com/2010/01/1131/</guid>
		<description><![CDATA[使用URLRequest下载文件. &#8211; 无权限认证


// 下载文件
	protected function downloadTemplateFile():void {
		var fileRef:FileReference = new FileReference();
		var u<p class='read-more'><a href='http://liguoliang.com/2010/flex-%e4%bd%bf%e7%94%a8urlrequest%e8%bf%9b%e8%a1%8c%e4%b8%8b%e8%bd%bd-%e5%b9%b6%e7%9b%91%e5%90%ac%e6%9c%89%e5%85%b3%e4%ba%8b%e4%bb%b6/'>More...</a></p><p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></description>
			<content:encoded><![CDATA[<p>使用URLRequest下载文件. &#8211; 无权限认证
</p>
<pre class="java" name="code">
// 下载文件
	protected function downloadTemplateFile():void {
		var fileRef:FileReference = new FileReference();
		var urlReq:URLRequest = new URLRequest(_pathTemplateFile);
		fileRef.addEventListener(Event.OPEN,onDownloadBegin);
		fileRef.addEventListener(Event.COMPLETE, onDownloadComplete);
		fileRef.addEventListener(Event.CANCEL, onDownloadCancel);
		fileRef.addEventListener(IOErrorEvent.IO_ERROR, onDownloadError);
		fileRef.download(urlReq);
	}

	// 模板文件下载开始时响应.
	protected function onDownloadBegin(e:Event):void {
		appendTextToOutPut(RM.getString(BUNDLE_ASMT_MGT, &quot;template.download.begin&quot;));
	}

	// 当下载结束后响应. */
	protected function onDownloadComplete(e:Event):void {
		var file:FileReference = e.target as FileReference;
		appendTextToOutPut(RM.getString(BUNDLE_ASMT_MGT, &quot;template.download.success&quot;, [file.name, FileUtils.formatFileSize(file.size)]));
		log.info(&quot;模板文件下载结束: &quot; + file.name + &quot;-&quot; + file.size);
	}

	// 当下载取消时响应. */
	protected function onDownloadCancel(e:Event):void {
		appendTextToOutPut(RM.getString(BUNDLE_ASMT_MGT, &quot;controller.download.canceled&quot;));
	}

	// 当下载出现错误时响应. */
	protected function onDownloadError(e:IOError):void {
		appendTextToOutPut(RM.getString(BUNDLE_ASMT_MGT, &quot;template.download.error&quot;, [e.message]));
		log.error(&quot;下载出错&quot; + e.message);
	}</pre>
<p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></content:encoded>
			<wfw:commentRss>http://liguoliang.com/2010/flex-%e4%bd%bf%e7%94%a8urlrequest%e8%bf%9b%e8%a1%8c%e4%b8%8b%e8%bd%bd-%e5%b9%b6%e7%9b%91%e5%90%ac%e6%9c%89%e5%85%b3%e4%ba%8b%e4%bb%b6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

