Example of Calling C # written Web Services in Delphi

Source: Internet
Author: User

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.