A recent project used Delphi to call the Web Service written in C. It took a day to find the reason and finally the test passed.
This is a way to upload a photo to a specified location on the website through the web service. After testing for n times, it was unsuccessful. After tracking, we found that Delphi did not pass any value at all. Through data query, we found that vs2005 uses soapdocumentprotocol by default and Delphi uses soaprpcprotocol. this will cause changes to the strings uploaded by the client.
To be null, add the following statement to the initialization part of the generated interface unit: invregistry. registerinvokeoptions (typeinfo (servicesoap), iodocument );
Done.
Service Code:
[Webmethod]
Public int uploadfile (byte [] FS, string filename)
{
Try
{
/// Define and instantiate a memory stream to store the submitted byte array.
/// Define the actual object and save the uploaded object.
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 (0, sofromend );
Setlength (result, isize );
FS. Seek (0, 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. The service address can be dynamically changed during the program running. I didn't know this before.
Mywsupfile: = getwsupfilesoap (true, defwsdl, httprio1 );
// Mywsupfile: = httprio1 as wsupfilesoap;
Try
Try
Case (mywsupfile. uploadfile (FS, filename)
0: myshowmessage ('user has no authorization ');
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 the Web service! ');
End;
End;