Special Report

ขอ In-Depth ด้วยเรื่องเบาๆล่ะกัน(อ้าว) อันนี้ผมเคยลงใน Blog ของผมนานมากๆแล้วครับ แต่คิดว่ามีประโยชน์และเข้ากับคอนเซปท์ของ In-Depth เลยเอามาลง

คือในหน่วยงานของลูกค้าเ่ก่าของผมนั้น จะมี Applicaion หลายๆตัว เชื่อมต่อกันครับ เขียนด้วยหลายภาษาซะด้วยทั้ง Java,VB.X,C++ และผมก็ได้รับงานให้เขียน VB.NET ไปเรียก Java Servlet ก็เลยค้นๆและวานคนอื่นมาช่วยๆกัน เลยได้มาครับ

Imports System.Collections.Specialized
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(...)
Try
Dim url As String
url = txtUrl.Text '<----- url ของ servlet ครับ
Dim para(3) As String
para(0) = txtSubr.Text.Trim '<------ ค่า parameter
para(1) = txtService.Text.Trim '<------ ค่า parameter
para(2) = txtMess.Text.Trim '<----- ค่า parameter
Dim result As String = PostData(url, para)
txtResult.Text = result '<----- result ที่ได้จาก servlet
Catch ex As Exception
txtResult.Text = ex.Message
End Try
End Sub
Function PostData(ByVal sAddress As String,
ByVal sData() As String) As String

Dim myNameValueCollection As New NameValueCollection
myNameValueCollection.Add("user", sData(0))
myNameValueCollection.Add("service", sData(1))
myNameValueCollection.Add("msg", aesEncrypt(sData(2)))
Dim wUpload As Net.WebClient = New System.Net.WebClient
Dim bResponse As Byte() = wUpload.UploadValues(sAddress, "POST",
myNameValueCollection)
Dim sWebPage As String = Encoding.ASCII.GetChars(bResponse)
Return sWebPage
End Function
End Class

โดย myNameValueCollection ไว้สำหรับเก็บค่าตัวแปรที่จะส่งให้กับ URL ครับ ส่วน myNameValueCollection.Add(XXX,value) นี่คือการกำหนดค่าตัวแปรต่างๆที่จะส่งให้กับ URL

จุดสำคัญคือการใช้ Obj WebClient ครับ และใช่ method UploadValues ส่งตัวแปรห้ดังนี้ (address,method,querycollection) โดย addesss เป็น string url ครับ
ส่วน method ก็คือ "GET"/"POST" นั่นเอง

ส่วน querycollection ก็คือ collection obj ที่ส่งค่าตัวแปรไปนั่นเองโดยต้อง map ชื่อกะค่าให้ตรงกับฝั่ง servlet ด้วยครับ querycollection ที่ผมใช้คือ NameValueCollection ซึ่งต้องการ namespace System.Collections.Specialized ครับ

ว่าแล้วก็จะได้ result ที่ทาง servlet out.print ออกมาครับ(ถ้า servlet set contenttype เป็น html จะมี tag html ติดมาด้วย)

Hiring! บริษัทที่น่าสนใจ

Carmen Software company cover
Carmen Software
Hotel Financial Solutions
Next Innovation (Thailand) Co., Ltd. company cover
Next Innovation (Thailand) Co., Ltd.
We are web design with consulting & engineering services driven the future stronger and flexibility.
KKP Dime company cover
KKP Dime
KKP Dime บริษัทในเครือเกียรตินาคินภัทร
Kiatnakin Phatra Financial Group company cover
Kiatnakin Phatra Financial Group
Financial Service
Fastwork Technologies company cover
Fastwork Technologies
Fastwork.co เว็บไซต์ที่รวบรวม ฟรีแลนซ์ มืออาชีพจากหลากหลายสายงานไว้ในที่เดียวกัน
Thoughtworks Thailand company cover
Thoughtworks Thailand
Thoughtworks เป็นบริษัทที่ปรึกษาด้านเทคโนโยลีระดับโลกที่คว้า Great Place to Work 3 ปีซ้อน
Iron Software company cover
Iron Software
Iron Software is an American company providing a suite of .NET libraries by engineer for engineers.
CLEVERSE company cover
CLEVERSE
Cleverse is a Venture Builder. Our team builds several tech companies.
Nipa Cloud company cover
Nipa Cloud
#1 OpenStack cloud provider in Thailand with our own data center and software platform.
Bangmod Enterprise company cover
Bangmod Enterprise
The leader in Cloud Server and Hosting in Thailand.
CIMB THAI Bank company cover
CIMB THAI Bank
MOVING FORWARD WITH YOU - CIMB is the leading ASEAN Bank
Bangkok Bank company cover
Bangkok Bank
Bangkok Bank is one of Southeast Asia's largest regional banks, a market leader in business banking
MuvMi (Urban Mobility Tech Co.,Ltd.) company cover
MuvMi (Urban Mobility Tech Co.,Ltd.)
Shape the future of urban mobility towards affordable, clean, and safe solutions
T.N. Digital Solution Co., Ltd. company cover
T.N. Digital Solution Co., Ltd.
TNDS has been involving in every first move of banking’s major digital transformation.
KBTG - KASIKORN Business-Technology Group company cover
KBTG - KASIKORN Business-Technology Group
KBTG - "The Technology Company for Digital Business Innovation"
Siam Commercial Bank Public Company Limited company cover
Siam Commercial Bank Public Company Limited
"Let's start a brighter career future together"
Icon Framework co.,Ltd. company cover
Icon Framework co.,Ltd.
Global Standard Platform for Real Estate แพลตฟอร์มสำหรับธุรกิจอสังหาริมทรัพย์ครบวงจร มาตรฐานระดับโลก
REFINITIV company cover
REFINITIV
The Financial and Risk business of Thomson Reuters is now Refinitiv
H LAB company cover
H LAB
Re-engineering healthcare systems through intelligent platforms and system design.
The Gang Technology Co., Ltd. company cover
The Gang Technology Co., Ltd.
We're a Digital Agency that helps our customers transform their business into digital with ease.
LTMH company cover
LTMH
LTMH มุ่งเน้นการพัฒนาผลิตภัณฑ์ที่สามารถช่วยพันธมิตรของเราให้บรรลุเป้าหมาย
Seven Peaks company cover
Seven Peaks
We Drive Digital Transformation
Wisesight (Thailand) Co., Ltd. company cover
Wisesight (Thailand) Co., Ltd.
The Best Choice For Handling Social Media · High Expertise in Social Data · Most Advanced and Secure
MOLOG Tech company cover
MOLOG Tech
We are Modern Logistic Platform, Specialize in WMS, OMS and TMS.
Data Wow Co.,Ltd company cover
Data Wow Co.,Ltd
We enable our clients to realize increased productivity by solving their most complex issues by Data
LINE Company Thailand company cover
LINE Company Thailand
LINE, the world's hottest mobile messaging platform, offers free text and voice messaging + Call
LINE MAN Wongnai company cover
LINE MAN Wongnai
Join our journey to becoming No.1 food platform in Thailand

lew Thu, 11/05/2006 - 10:54

ขออนุญาตแก้นิดหน่อยนะครับ เรื่องการจัดสี กับขอเอาออกจากหน้าแรกมาไว้หน้า In-Depth เหมือนๆ กันครับ

plynoi Thu, 11/05/2006 - 11:11

อ่า จะจัดการสีนี่ทำไงอ่ะคับ? (ว่าจะถามอยู่พอดี)

lew Thu, 11/05/2006 - 12:12

ใส่แอทริบิวต์เป็นชื่อภาษาครับ อย่าง VB.NET ก็ใช้ lang="vbnet"

deans4j Fri, 12/05/2006 - 07:42

ไม่น่าจะตั้งชื่อว่าเรียก Java Servlet นะครับ เพราะจากเท่าที่ดูตัวอย่างคือการใช้เรียก URL ทั่วไปครับ

chakrit Sat, 13/05/2006 - 18:05

น่าจะใส่ try catch ที่ postdata ด้วยนะครับ

แล้วก็ลืมใส่ End Class รึเปล่าคับ??

plynoi Sat, 13/05/2006 - 18:21

deans4j,Kerberos มัีนน่าจะเรียก url method ทั่วๆไปได้มั้ง แต่ตอนนั้นทำเรียก Servlet อย่างเดียว chakrit น่าจะใส่แหละ แต่ End Class ตกไปเพราะ copy มาไม่หมด :P