How to call webservice in delphi
This article shows you how to call webservice in delphi. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
1. Put the URL address of WSDL in the WSDLLocation parameter of HTTPRIO, and then select service and port
two。 A recent project uses the ability of delphi to invoke web services written by C #. It took a day to find out the reason, and finally the test passed.
This is a way to upload a photo to a specified location on the site through the web service. First tested N times are unsuccessful, through tracking found that delphi did not send any value, by looking up the data found that VS2005 default is the use of SoapDocumentProtocol and Delphi is the use of SoapRpcProtocol. This will cause all the strings passed by the client to become null. You need to add a sentence to the INITIALIZATION part of the generated interface unit: InvRegistry.RegisterInvokeOptions (TypeInfo (ServiceSoap), ioDocument).
It's done.
Service Code:
[WebMethod]
Public int UploadFile (byte [] fs, string FileName)
{
Try
{
/ / define and instantiate a memory stream to hold the submitted byte array.
/ / define the actual file object and save the uploaded file.
String FullFilename = Server.MapPath ("photo\") + FileName
If (File.Exists (FullFilename))
{
File.Delete (FullFilename)
}
FileStream f = new FileStream (FullFilename, FileMode.Create, FileAccess.Write)
F.Write (fs, 0, fs.Length)
F.Close ()
F = null
Return 1
}
Catch
{
Return 2
}
}
Delphi client code:
Procedure TFrmUpLoad.btn2Click (Sender: TObject)
Function ReadFile (fileName:string): TByteDynArray
Var
Fs:TFileStream
ISize:Int64
Begin
Try
Fs:=TFileStream.Create (fileName,fmOpenRead)
ISize:=fs.Seek (0mensoFromEnd)
SetLength (Result,iSize)
Fs.Seek (0m soFromBeginning)
Fs.ReadBuffer (Result [0], iSize)
Finally
Fs.Free
End
End
Var
DefWSDL,defURL,defSvc,defPrt: string
MyWSUpFile:WSUpFileSoap
Fs: TByteDynArray
FileName:string
Begin
Fs:=ReadFile (edt1.Text)
DefWSDL: = 'http://'+sysinfo.WZ+'/wsupfile.asmx?wsdl';
DefURL: = 'http://'+sysinfo.WZ+'/wsupfile.asmx';
DefSvc: = 'WSUpFile'
DefPrt: = 'WSUpFileSoap'
HTTPRIO1.WSDLLocation:=defWSDL
HTTPRIO1.Port:=defPrt
HTTPRIO1.URL:=defURL
HTTPRIO1.Service:=defSvc
HTTPRIO1.HTTPWebNode.UseUTF8InHeader:=true
FileName:=HYBH+ExtractFileExt (edt1.Text)
/ / pay attention to this line, you can change the service address dynamically while the program is running, which I didn't know before.
MyWSUpFile:=GetWSUpFileSoap (True,defWSDL,HTTPRIO1)
/ / MyWSUpFile:=HTTPRIO1 as WSUpFileSoap
Try
Try
Case (MyWSUpFile.UploadFile (fs,fileName)) of
0:MyShowMessage ('user does not have permissions')
1:begin
FrmHYGL.ADOQuery1.Edit
FrmHYGL.ADOQuery1.FieldByName ('ZP'). AsString:=fileName
FrmHYGL.ADOQuery1.Post
MyShowMessage ('upload successful')
End
2:MyShowMessage ('upload failed')
End
Finally
MyWSUpFile:=nil
End
Except
MyShowMessage ('failed to call web service!')
End
End
The above is how to call webservice in delphi. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.