How to use HttpWebRequest to invoke a web service operation that takes a byte [] parameter?

I am trying to call [webmethod] from C #. I can name a simple web method that takes "string" parameters. But I have a web method that accepts the "byte []" parameter. When I try to call it, I run an "internal server error". Here is an example of what I am doing.

Let's say my method is like this

[WebMethod]
public string TestMethod(string a)
{
    return a;
}

I call it that way using HttpRequest in C #

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Credentials = CredentialCache.DefaultCredentials;
            req.Method = "POST";
            // Set the content type of the data being posted.
            req.ContentType = "application/x-www-form-urlencoded";

            string inputData = "sample webservice";
            string postData = "a=" + inputData;
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] byte1 = encoding.GetBytes(postData);

            using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
            {
                StreamReader sr = new StreamReader(res.GetResponseStream());
                string txtOutput = sr.ReadToEnd();
                Console.WriteLine(sr.ReadToEnd());
            }

This works great. Now I have another web method that is defined as

[WebMethod]
public string UploadFile(byte[] data)

I tried calling him like that

            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = "data=abc";
            byte[] sendBytes = encoding.GetBytes(postData);
            req.ContentLength = sendBytes.Length;
            Stream newStream = req.GetRequestStream();
            newStream.Write(sendBytes, 0, sendBytes.Length);

But this gives me 500 internal errors :(

+5
source share
4

,

, , - . Chrome. - (.. Http://myweb.com/WS/MyWS.asmx? Op = Validation)

WebRequest request = WebRequest.Create(http://myweb.com/WS/MyWS.asmx?op=Validation);
request.Method = "POST";
((HttpWebRequest)request).UserAgent = ".NET Framework Example Client";
request.ContentType = "text/xml; charset=utf-8";
((HttpWebRequest)request).Referer = "http://myweb.com/WS/MyWS.asmx?op=Validation";
((HttpWebRequest)request).Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
((HttpWebRequest)request).Host= "myweb.com";
 request.Headers.Add("SOAPAction","http://myweb.com/WS/Validation");

string message = "a=2";
string envelope = "<?xml version=\"1.0\" encoding=\"utf-8\"?><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/\">"+
        "<soap:Body><Validation xmlns=\"http://myweb.com/WS\"><data>@Data</data></Validation></soap:Body></soap:Envelope>";
string SOAPmessage = envelope.Replace("@Data",   System.Web.HttpUtility.HtmlEncode(message));
// The message must be converted to bytes, so it can be sent by the request
byte[] data = Encoding.UTF8.GetBytes(SOAPmessage);
request.ContentLength = data.Length;
request.Timeout = 20000;
Stream dataStream = request.GetRequestStream();
dataStream.Write(data, 0, data.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Stream inputStream = response.GetResponseStream(); 

SOAP , , , , - (.. http://myweb.com/WS/MyWS.asmx? op = Validation).

+5

HTTP POST/HTTP GET ASP.NET Web Service -. , -, . , URL- -, , . , , , , , , - -.

, , -. URL- - -. WCF, :

// ServiceNameClient is just a sample name, the actual name of your client will vary.
string data = "abc";
byte[] dataAsBytes = Encoding.UTF8.GetBytes(data);
ServiceNameClient client = new ServiceNameClient();
client.UploadFile(dataAsBytes);

, .

+3

, , base64.

500 - Windows , .

0

string postData = "data = abc";

Suppose you have to pass an array of bytes as an array, not a base64 string. eg:

string postData = "data = 97 & data = 98 & data = 99"; // byte array for abc - [97,98,99]

refer to https://www.codeproject.com/Tips/457410/Xml-WebService-Array-Parameters

0
source

All Articles