'network'에 해당되는 글 3건
- 2009/07/15 Silverlight - Network Sample 2
- 2009/07/13 Silverlight - Network Example 1
- 2009/04/30 Silverlight - Network (2)
자료제공 : http://hugeflow.com/
2009/04/30 - [.NET/SilverLight] - Silverlight - Network
2009/04/30 - [.NET/SilverLight] - Silverlight - Network Example 1
예제코드 :
3-2.WebServiceTest.zip |
이번에는 이전 내용과 마찬가지이지만 직접 가져오지 않고 웹 서비스를 만들어 데이터를 가져오도록 한다.
먼저 웹서비스의 내용에
/// <summary>
/// WebServiceTest의 요약 설명입니다.
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// ASP.NET AJAX를 사용하여 스크립트에서 이 웹 서비스를 호출하려면 다음 줄의 주석 처리를 제거합니다.
// [System.Web.Script.Services.ScriptService]
public class WebServiceTest : System.Web.Services.WebService
{[WebMethod]
public string HelloWorld()
{
return "Hello World";
}[WebMethod]
public string GetHtml(string address)
{
WebClient wc = new WebClient();
return wc.DownloadString(new Uri(address, UriKind.Absolute));
}
}
다음과 같은 내용을 작성하고,
만약 인코딩 문제로 문자가 깨지는 현상이 발생하면
다음과 같은 코드를 추가해준다.
wc.Encoding = System.Text.Encoding.UTF8;
Silverlight에서는 웹서비스 참조로 위 웹서비스를 추가해준다.
그리고 실버라이트의 Page에 코드를 작성한다..
먼저 Page.xaml 코드
<UserControl x:Class="WebServiceTest.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="485" d:DesignHeight="377">
<Grid x:Name="LayoutRoot" Background="White">
<TextBox Height="47" Margin="8,8,100,0" VerticalAlignment="Top" Text="http://hugeflow.com/sample.xml" TextWrapping="Wrap" x:Name="tboxAddress"/>
<Button HorizontalAlignment="Right" Margin="0,8,8.192,0" VerticalAlignment="Top" Content="Get" Width="88" Height="47" x:Name="buttonGet" Click="buttonGet_Click"/>
<TextBlock Margin="8,59,8,8" Text="" TextWrapping="Wrap" x:Name="tbOutput"/>
</Grid>
</UserControl>
C# 코드
using System;
using System.Windows;
using System.Windows.Controls;namespace WebServiceTest
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}private void buttonGet_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(tboxAddress.Text) == true)
{
return;
}try
{
WebServiceTest.ServiceReference1.WebServiceTestSoapClient wsclient = new WebServiceTest.ServiceReference1.WebServiceTestSoapClient();
wsclient.GetHtmlCompleted += new EventHandler<WebServiceTest.ServiceReference1.GetHtmlCompletedEventArgs>(wsclient_GetHtmlCompleted);
wsclient.GetHtmlAsync(tboxAddress.Text);
}
catch (Exception ex)
{
tbOutput.Text = ex.Message;
}
}void wsclient_GetHtmlCompleted(object sender, WebServiceTest.ServiceReference1.GetHtmlCompletedEventArgs e)
{
if (e.Cancelled == true)
{
tbOutput.Text = "취소되었습니다.";
return;
}if (e.Error != null)
{
if (string.IsNullOrEmpty(e.Error.Message) == false)
{
tbOutput.Text = e.Error.Message;
}
else
{
tbOutput.Text = e.Error.ToString();
}
return;
}tbOutput.Text = e.Result;
}
}
}
이전 예제와 대부분이 비슷한 것을 알 수 있다.
그냥 웹서비스 호출하도로 바꾼내용이니.. ㅎㅎ
'.NET > Silverlight' 카테고리의 다른 글
| Silverlight - Media Player Sample (2) | 2009/07/20 |
|---|---|
| Silverlight + Expression = Visual Kitchen (0) | 2009/07/16 |
| Silverlight - Network Sample 2 (0) | 2009/07/15 |
| Silverlight - Network Example 1 (0) | 2009/07/13 |
| Silverlight – Custom control (0) | 2009/07/03 |
| Silverlight Custom control (0) | 2009/06/23 |
자료제공 : http://hugeflow.com/
3-1.WebClientTest.zip |
이전 내용에 Silverlight의 네트워크에 작성한 내용이 있다.
2009/04/30 - [.NET/SilverLight] - Silverlight - Network
이 내용을 바탕으로 다음의 소스코드를 작성하였으므로 내용의 이해가 조금 부족하면 확인하시길...
예제는 프로젝트명 WebClientTest로 하였고,
먼저 Page.xaml 코드는
<UserControl x:Class="WebClientTest.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="Auto" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="418">
<Grid x:Name="LayoutRoot" Background="White"><TextBox Height="47" Margin="8,8,100,0" VerticalAlignment="Top" Text="http://hugeflow.com/sample.xml" TextWrapping="Wrap" x:Name="tboxAddress"/>
<Button HorizontalAlignment="Right" Margin="0,8,8.192,0" VerticalAlignment="Top" Content="Get" Width="88" Height="47" x:Name="buttonGet" Click="buttonGet_Click"/>
<TextBlock Margin="8,59,8,8" Text="" TextWrapping="Wrap" x:Name="tbOutput"/></Grid>
</UserControl>
다음으로 C# 코드
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;namespace WebClientTest
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}private void buttonGet_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(tboxAddress.Text) == true)
{
return;
}try
{
WebClient wc = new WebClient();
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri(tboxAddress.Text, UriKind.Absolute));
}
catch (Exception ex)
{
tbOutput.Text = ex.Message;
throw;
}
}void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Cancelled == true)
{
tbOutput.Text = "취소되었습니다.";
return;
}if (e.Error != null)
{
if (string.IsNullOrEmpty(e.Error.Message) == false)
{
tbOutput.Text = e.Error.Message;
}
else
{
tbOutput.Text = e.Error.ToString();
}
return;
}tbOutput.Text = e.Result;
}
}
}
'.NET > Silverlight' 카테고리의 다른 글
| Silverlight + Expression = Visual Kitchen (0) | 2009/07/16 |
|---|---|
| Silverlight - Network Sample 2 (0) | 2009/07/15 |
| Silverlight - Network Example 1 (0) | 2009/07/13 |
| Silverlight – Custom control (0) | 2009/07/03 |
| Silverlight Custom control (0) | 2009/06/23 |
| Silverlight – bubbling events (2) | 2009/05/18 |
자료 출처 : http://hugeflow.com/
실버라이트 2.0에는 동기방식의 통신은 없애고 비동기 통신만 남겨두었다.
이 방식을 사용하기에 WebClient와 WebRequest 두가지를 사용할 수 있는데.
WebClient가 약간 더 코드가 간결하고 WebRequest는 어렵다. 하지만 크게 어려운 것은 아니다.
다음의 기본 사용 예로 설명을 대신하도록 한다.
WebClient
WebClient wc = new WebClient();
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri("http://hugeflow.com/sample.xml", UriKind.RelativeOrAbsolute));
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
string result = e.Result;
}
HttpWebRequest
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://hugeflow.com/sample.xml", UriKind.Absolute));
request.BeginGetRequestStream(new AsyncCallback(ReadCallback), request);
private void ReadCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string result = reader.ReadToEnd();
}
}
'.NET > Silverlight' 카테고리의 다른 글
| Silverlight - Access HTML document (0) | 2009/05/07 |
|---|---|
| Silverlight - Access managed code form javascript (0) | 2009/05/05 |
| Silverlight - Network (2) | 2009/04/30 |
| Silverlight domain access control (0) | 2009/04/28 |
| REMIX08 RIA, UX의 세계 (0) | 2008/06/16 |
| Programming Silverlight with JavaScript (0) | 2008/05/28 |

3-2.WebServiceTest.zip
이올린에 북마크하기
이올린에 추천하기



Prev




