⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wsfromjava.aspx.htm

📁 WebService_fromJava_demo
💻 HTM
📖 第 1 页 / 共 5 页
字号:
		</tbody></table>
		</td>
</tr>
<tr id="ctl00_ArticleRating_PopularityRow">
			<td colspan="2" class="SmallText" align="right">
<a id="ctl00_ArticleRating_PopularityLnk" title="Calculated as rating x Log10(# votes)" href="http://www.codeproject.com/script/Articles/TopArticles.aspx?ta_so=1">Popularity: 5.22</a>
<span id="ctl00_ArticleRating_PopularityLbl"></span><span id="ratingVal">Rating: <b>4.08</b> out of 5</span></td>
		</tr>
		
</tbody></table>



</td>
				<td>            
<div>
<table class="HistTable" title="Voting Distribution. Recent data only"><tbody><tr><td><img src="WSfromJava.aspx_files/pollcol.gif" alt="2 votes, 10.5%" title="2 votes, 10.5%" border="0" height="3" width="10"><br>1</td>
<td><img src="WSfromJava.aspx_files/pollcol.gif" alt="2 votes, 10.5%" title="2 votes, 10.5%" border="0" height="3" width="10"><br>2</td>
<td><img src="WSfromJava.aspx_files/t.htm" alt="0 votes, 0.0%" title="0 votes, 0.0%" border="0" height="1" width="10"><br>3</td>
<td><img src="WSfromJava.aspx_files/pollcol.gif" alt="3 votes, 15.8%" title="3 votes, 15.8%" border="0" height="5" width="10"><br>4</td>
<td><img src="WSfromJava.aspx_files/pollcol.gif" alt="12 votes, 63.2%" title="12 votes, 63.2%" border="0" height="20" width="10"><br>5</td>
</tr></tbody></table>
</div>



</td></tr></tbody></table>
				</td>
			</tr>
			</tbody></table>
			
			
			
			
						
			
			
			
			
			
			
			
			
			<span id="intelliTXT">
			<div id="contentdiv">
			
			<!-- Main Page Contents Start -->
			

<!-- Article Starts -->

<ul class="download">
<li><a href="http://www.codeproject.com/KB/XML/WSfromJava/WSfromJava_demo.zip">Download demo project - 14.6 Kb</a> </li></ul>
<p><img alt="Sample Image - WSfromJava.jpg" src="WSfromJava.aspx_files/WSfromJava.jpg" height="274" width="490"></p>
<h2>Introduction</h2>
<p>This article will be useful if you need to consume ASP.NET
webservices from languages or platforms without .NET support. In this
example, I am consuming a webservice from a Java applet, but it would
take very little work to adapt my code to run as a Java application.
Although I have only tested this on Windows XP, the code should run on
other platforms, providing cross-platform client-side consumption of
ASP.NET webservices.</p>
<p>I developed this solution because I needed to add complex
client-side processing to my ASP.NET application. I did not want to
have to cope with all of the slightly different JavaScript
implementations used by the main browsers, and this led me to consider
a Java applet.</p>
<p>This article shows you how to use a Java applet in your webpage and
get it to communicate back to ASP.NET by consuming an XML webservice on
the client.</p>
<h2>Background</h2>
<p>There are, no doubt, many reasons why somebody might want
cross-platform consumption of ASP.NET webservices, but this is my story:</p>
<p>I have an ASP.NET application where I needed to add client side
image manipulation facilities. I looked into writing the client-side
code in JavaScript which seemed like the obvious choice, but was
frustrated by different implementations in the browsers, especially
when trying to use image manipulation features. For example, if you
want to alpha blend your image, you would have to use <code>img.style.MozOpacity</code> in Netscape, <code>img.filters.alpha.opacity</code>
in Internet Explorer and there is no way to do this at all in Opera. I
really didn't want to have to write special code for each browser or
downgrade the functionality to the lowest common denominator.</p>
<p>Therefore, I decided to try writing my client-side code in Java 1.1.
Note that I'm using the Java 1.1 SDK, as Internet Explorer currently
does not support code developed with Java SDKs after 1.1.4 (although it
seems to me that Java SDK 1.1.7 code seems to work OK in IE). A Java
applet can do all of the image manipulation I needed and is supported
by many browsers. Finally, I don't have to write separate code for each
browser.</p>
<p>The next problem was, how do I get the result of the data processing
done in the Java applet back into my ASP.NET application? The answer
that I came up with was to create an ASP.NET webservice and consume it
in the Java applet.</p>
<h2>A Simple Example</h2>
<p>Take a look at the following very basic webservice:</p><div class="SmallText" id="premain0" style="width: 100%; cursor: pointer;"><img preid="0" src="WSfromJava.aspx_files/minus.gif" id="preimg0" height="9" width="9"><span preid="0" style="margin-bottom: 0pt;" id="precollapse0"> Collapse</span></div><pre style="margin-top: 0pt;" id="pre0" lang="cs">    [WebMethod]
    <span class="code-keyword">public</span> <span class="code-keyword">string</span> ConcatWithSpace(<span class="code-keyword">string</span> one, <span class="code-keyword">string</span> two)
    {
        <span class="code-keyword">return</span> one+<span class="code-string">"</span><span class="code-string"> "</span>+two;
    }</pre>
<p>It could be consumed very simply by making the Java applet perform a request to: <i>webservice.asmx/ConcatWithSpace?one=FirstValue&amp;two=SecondValue</i>...
But what if we wanted to send larger amounts of data to the webservice?
How would we go about implementing a SOAP request and response in a
Java applet...? There are probably many options if you are using a more
recent Java SDK, but because I want to support many browsers including
Internet Explorer, I'm stuck with the Java 1.1 SDK. However, this gives
us a better opportunity to understand how to talk to ASP.NET web
services on other languages and platforms. Therefore, I have written my
own <code>SoapRequestBuilder</code> class in Java that makes it quite
easy to consume a service and return a single string response. Take a
look at this example, which consumes the webservice above:</p><div class="SmallText" id="premain1" style="width: 100%; cursor: pointer;"><img preid="1" src="WSfromJava.aspx_files/minus.gif" id="preimg1" height="9" width="9"><span preid="1" style="margin-bottom: 0pt;" id="precollapse1"> Collapse</span></div><pre style="margin-top: 0pt;" id="pre1" lang="jscript">    SoapRequestBuilder s = <span class="code-keyword">new</span> SoapRequestBuilder();
    s.Server = <span class="code-string">"</span><span class="code-string">127.0.0.1"</span>; <span class="code-comment">//</span><span class="code-comment"> server ip address or name
</span>
    s.MethodName = <span class="code-string">"</span><span class="code-string">ConcatWithSpace"</span>;
    s.XmlNamespace = <span class="code-string">"</span><span class="code-string">http://tempuri.org/"</span>;
    s.WebServicePath = <span class="code-string">"</span><span class="code-string">/SimpleService/Service1.asmx"</span>;
    s.SoapAction = s.XmlNamespace+s.MethodName;
    s.AddParameter(<span class="code-string">"</span><span class="code-string">one"</span>, <span class="code-string">"</span><span class="code-string">David"</span>);
    s.AddParameter(<span class="code-string">"</span><span class="code-string">two"</span>, <span class="code-string">"</span><span class="code-string">Hobbs"</span>);
    String response = s.sendRequest();</pre>
<p>In this example, the <b>response</b> string is filled with the result from the webservice after we sent it two parameters. The full source code of the <code>SoapRequestBuilder</code> class is included in the source files.</p>
<p>That's it! It's only a very simple example and at the moment, I'm
only interested in returning a single value back from the webservice,
but I have used something similar with a webservice that talks to my
back-end database and this allows my Java applet to store data which
can then be accessed by the ASP.NET pages.</p>
<p>To run the example code, set up the webservice first, then copy the
Java applet files to any folder on the same machine and double-click
the HTML file. All of the parameters are configured inside the HTML
file, so you can change the values passed to the webservice by editing
the HTML; you should not have to re-compile the Java source. I have
tested the example in IE 6.0, Netscape 7.1 and Opera 7.23 on Windows XP.</p>
<h2>Notes</h2>
<p>Some Java Virtual Machines (like the Microsoft one) only allow you
to make a socket connection to the same machine that hosts the Java
class files. Therefore, if you're using an applet like me, you will
need to host the Java class files on the same machine where the
webservice resides.</p>



<!-- Article Ends -->


			<!-- Main Page Contents End -->
			
			</div>
			</span>
			
			<form name="aspnetForm" method="post" action="WSfromJava.aspx" id="aspnetForm" style="margin: 0pt; padding: 0pt;">
		<div>
		<input name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTEwMDUyNjYzMjhkZN5TmTruKMZhS+9MLL6nvZ9lrE3d" type="hidden">
		</div>
		

			
			<h2>License</h2>
			<div id="ctl00_LicenseTerms"><p>This
article has no explicit license attached to it but may contain usage
terms in the article text or the download files themselves. If in doubt
please contact the author via the discussion board below.</p><p>A list of licenses authors might use can be found <a href="http://www.codeproject.com/info/Licenses.aspx">here</a></p></div>
			
			<h2>About the Author</h2>
			
					
            
<table border="0" cellpadding="0" cellspacing="5" width="100%">
<tbody><tr valign="top">
<td id="ctl00_AboutAuthorRptr_ctl00_AboutAuthor_memberPhotoTable" style="width: 155px;" valign="top">
	<b><a id="ctl00_AboutAuthorRptr_ctl00_AboutAuthor_memberProfileLink" href="http://www.codeproject.com/script/Membership/Profiles.aspx?mid=257552">David S Hobbs</a></b><br><br>
	<center><img id="ctl00_AboutAuthorRptr_ctl00_AboutAuthor_memberPhoto" src="WSfromJava.aspx_files/D02E471B-4EAE-4337-A238-FA58CD4A2F7E.jpg" style="border-width: 0px;"></center><br>
	<span id="ctl00_AboutAuthorRptr_ctl00_AboutAuthor_memberType" class="SmallText"></span>
		
</td>
		
<td> I have been developing software on various platforms for over 14
years. For my sins I recently spent two years managing a development
team at some large Redmond-based software company....<br><br>I'm
now enjoying working as a freelance software developer from home,
although I seem to be working harder than ever. My main area of work is
ASP.Net development with C#.<br><br>I like to take as many holidays as
possible, but always end up taking a book on software development with

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -