VB call Web Service

It's easy to call web service in C# code,but how about it for VB code? The following code will show you how to call web service in VB code.

Microsoft XML,v6.0 must be added in reference in this post.

Below codes show one method to call web service:

****************Web Service*********************

[WebMethod]
public XmlDataDocument getLoadIDInfo3(string strSHPDAT)
{
Recordset rs = new Recordset();
Connection cn = new Connection();
Stream st = new Stream();
XmlDataDocument xd = new XmlDataDocument();
string strsql = "SELECT * FROM LOAT WHERE SHPDAT='" + strSHPDAT + "'";
String myConnString = "Provider=sqlOLEDB.1;User ID=xxx;password=xxxxx;Initial Catalog=DB name;Data Source=172.20.xxx.xxx";
cn.Open(myConnString,"",0);
rs.ActiveConnection = cn;
rs.Open(strsql,cn,ADODB.CursorTypeEnum.adOpenKeyset,ADODB.LockTypeEnum.adLockReadOnly,0);
rs.Save(st,PersistFormatEnum.adPersistXML);
st.Flush();
st.Position = 0;
xd.LoadXml(st.ReadText(st.Size));

rs.Close();
cn.Close();
return xd;
}

&&&&&&&&&&&&&& VB6.0 call Web service &&&&&&&&&&&&&&&&&

Dim oHTTP As XMLHTTP
Dim oXmlDoc As DOMDocument
Dim rs As New ADODB.Recordset
Dim strURL As String,strRequest As String

strURL = "http://localhost:22090/ws/Service.asmx/getLoadIDInfo3"
strRequest = "strSHPDAT=20101105"
Set oHTTP = New XMLHTTP
oHTTP.Open "POST",strURL,False
oHTTP.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
oHTTP.send (strRequest)

Set oXmlDoc = New DOMDocument
oXmlDoc.Load oHTTP.responseXML
rs.Open oXmlDoc
While Not rs.EOF
MsgBox rs("LOADID") & " " & rs("SHPDAT") & " " & rs("LOADQT") & " " & rs("TRKMAS")
rs.MoveNext
Wend


Below codes show another method to call web service:

&&&&&&&&&&&&&& VB6.0 call Web service &&&&&&&&&&&&&&&&&

Private Sub Form_Load()
Dim oHTTP As XMLHTTP
Dim oXmlDoc As DOMDocument
Dim rs As New ADODB.Recordset
Dim soapMessage,soapData,URL
Dim strShipment As String
strShipment = "218|2010950940"
' Set the soap message
soapMessage = "<?xml version=""1.0"" encoding=""utf-8""?>"
soapMessage = soapMessage & "<soap:Envelope xmlns:xsi=""
http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">"
soapMessage = soapMessage & "<soap:Body>"
' Set the data for soap body ---- begin ------
soapData = "<Call_ForVB xmlns=""SAP.SD.Z_RFC_SEND_SHIPMENT_VB_ES"">"
soapData = soapData & " <strClientAndShipment>" & strShipment & "</strClientAndShipment>"
soapData = soapData & "</Call_ForVB>"
' Set the data for soap body ---- end ------
soapMessage = soapMessage & soapData & "</soap:Body>"
soapMessage = soapMessage & "</soap:Envelope>"
URL = "
http://10.243.9.16/SOA/SD/wsRFC_SEND_SHIPMENT_VB_ES.asmx" '可以使用相对地址或完整URL
' URL = "
http://10.243.9.16/SOA/SD/wsRFC_SEND_SHIPMENT_VB_ES.asmx?op=Call_ForVB"
Set oHTTP = CreateObject("Microsoft.XMLHTTP")
oHTTP.Open "POST",URL,False

oHTTP.setRequestHeader "Content-Type","text/xml; charset=utf-8"
oHTTP.setRequestHeader "Content-Length:",Len(soapMessage)
oHTTP.setRequestHeader "SOAPAction","SAP.SD.Z_RFC_SEND_SHIPMENT_VB_ES/Call_ForVB"
oHTTP.send (soapMessage)
Set oXmlDoc = New DOMDocument
oXmlDoc.Load oHTTP.responseXML
rs.Open oXmlDoc
While Not rs.EOF
MsgBox rs("Tknum") & " " & rs("Matnr") & " " & rs("Kdmat") & " " & rs("Erdat") & " " & rs("Lfimg")
rs.MoveNext
Wend
'那么如何知道是否调用成功呢,状态为200说明调用成功,500则说明出错
MsgBox (oHTTP.Status)
MsgBox (oHTTP.statusText)

Unload Me
End Sub

****************Web Service*********************

[WebMethod]
public XmlDataDocument Z_RFC_SEND_SHIPMENT_VB_ES_For_VB(string strClientAndShipment)
{
XmlDataDocument xd = new XmlDataDocument();
xd.CreateXmlDeclaration("1.0","UTF-8",String.Empty);
Recordset rs = new Recordset();
Stream st = new Stream();
DataSet dsReturn = new DataSet(); //dsreturn for Return DataSet
wsSAPProxy objSap = new wsSAPProxy();
objSap.Connection = new SAPConnection(GetSAPProxy(strClientAndShipment.Split('|')[0]));
ZJOWTable wsZJOW = new ZJOWTable();
objSap.Z_Rfc_Send_Shipment_Vb_Es(strClientAndShipment.Split('|')[1],ref wsZJOW);

DataTable dtReturn = new DataTable();
dtReturn = wsZJOW.ToADODataTable();
rs = ConvertToRecordset(dtReturn);
rs.Save(st,PersistFormatEnum.adPersistXML);
st.Flush();
st.Position = 0;
xd.LoadXml(st.ReadText(st.Size));
return xd;
}
private string GetSAPProxy(string strCLIENT)
{

string strReturnSAPProxy = "";
InitRemoting.SetsqlClient();
obj = InitRemoting.objsqlClient;

try
{
strReturnSAPProxy = obj.GetSAPConnString(strCLIENT);
}

catch
{
SAPAccess objSAPAccess = null;
objSAPAccess = new SAPAccess(strCLIENT,@"SapConfig.xml");
objSAPAccess.ErrorFlag = 0;

ArrayList aryConnectionString = objSAPAccess.ConnectionString;
strReturnSAPProxy = aryConnectionString[0].ToString();
}

return strReturnSAPProxy;
}
static public ADODB.Recordset ConvertToRecordset(DataTable inTable)
{
ADODB.Recordset result = new ADODB.Recordset();
result.CursorLocation = ADODB.CursorLocationEnum.adUseClient;

ADODB.Fields resultFields = result.Fields;
System.Data.DataColumnCollection inColumns = inTable.Columns;

foreach (DataColumn inColumn in inColumns)
{
resultFields.Append(inColumn.ColumnName
,TranslateType(inColumn.DataType)
,inColumn.MaxLength
,inColumn.AllowDBNull ? ADODB.FieldAttributeEnum.adFldIsNullable :
ADODB.FieldAttributeEnum.adFldUnspecified
,null);
}

result.Open(System.Reflection.Missing.Value
,System.Reflection.Missing.Value
,ADODB.CursorTypeEnum.adOpenStatic
,ADODB.LockTypeEnum.adLockOptimistic,0);

foreach (DataRow dr in inTable.Rows)
{
result.AddNew(System.Reflection.Missing.Value,
System.Reflection.Missing.Value);

for (int columnIndex = 0; columnIndex < inColumns.Count; columnIndex++)
{
string str = dr[columnIndex].ToString();
string str1 = str.Replace("/"","").Replace(",","").Replace("−","_").Replace("'","").Replace("#","").Replace("/","");
resultFields[columnIndex].Value = str1;
}
}

return result;
}
static ADODB.DataTypeEnum TranslateType(Type columnType)
{
switch (columnType.UnderlyingSystemType.ToString())
{
case "System.Boolean":
return ADODB.DataTypeEnum.adBoolean;

case "System.Byte":
return ADODB.DataTypeEnum.adUnsignedTinyInt;

case "System.Char":
return ADODB.DataTypeEnum.adChar;

case "System.DateTime":
return ADODB.DataTypeEnum.adDate;

case "System.Decimal":
return ADODB.DataTypeEnum.adCurrency;

case "System.Double":
return ADODB.DataTypeEnum.adDouble;

case "System.Int16":
return ADODB.DataTypeEnum.adSmallInt;

case "System.Int32":
return ADODB.DataTypeEnum.adInteger;

case "System.Int64":
return ADODB.DataTypeEnum.adBigInt;

case "System.SByte":
return ADODB.DataTypeEnum.adTinyInt;

case "System.Single":
return ADODB.DataTypeEnum.adSingle;

case "System.UInt16":
return ADODB.DataTypeEnum.adUnsignedSmallInt;

case "System.UInt32":
return ADODB.DataTypeEnum.adUnsignedInt;

case "System.UInt64":
return ADODB.DataTypeEnum.adUnsignedBigInt;

case "System.String": default: return ADODB.DataTypeEnum.adVarChar; } }

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强制返回为文本 --------------------------...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办法, Format 或者FormatDateTime 竟然结果和...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace My ‘全局错误处理,新的解决方案直接...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用的爽呀,这篇文章写与2011年,看来我以前没...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选中的单元格进行处理 Dim m As Range, t...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integ...