.net – 如何使用HttpWebRequest来调用接受byte []参数的Web服务 *** 作?

.net – 如何使用HttpWebRequest来调用接受byte []参数的Web服务 *** 作?,第1张

概述我试图从C#调用[webmethod].我可以调用带有’string’参数的简单webmethod.但我有一个webmethod接受’byte []’参数.当我尝试调用它时,我遇到了“500内部服务器错误”.这是我正在做的一些例子. 让我们说我的方法是这样的 [WebMethod]public string TestMethod(string a){ return a;} 我在C#中 我试图从C#调用[webmethod].我可以调用带有’string’参数的简单webmethod.但我有一个webmethod接受’byte []’参数.当我尝试调用它时,我遇到了“500内部服务器错误”.这是我正在做的一些例子.

让我们说我的方法是这样的

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

我在C#中使用httpRequest这样称呼它

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());            }

这完全没问题.现在我有另一个像这样定义的web方法

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

我试着这样称呼它

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

但这给了我一个500内部错误:(

@R_502_6120@ 有可能,我自己做了

首先是header设置,如果您的Web服务可以通过Web执行并发送参数,则可以获得此设置.我使用Chrome中的开发人员工具.简单的方法是查看webservice的描述(即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 requestbyte[] data = EnCoding.UTF8.GetBytes(SOAPmessage);request.ContentLength = data.Length;request.Timeout = 20000;Stream dataStream = request.GetRequestStream();dataStream.Write(data,data.Length);dataStream.Close();WebResponse response = request.GetResponse();Stream inputStream = response.GetResponseStream();

现在,您可以从响应中获取传入的流

请记住根据Web服务的页面详细信息(即http://myweb.com/WS/MyWS.asmx?op = ValIDation)给出的描述来调整SOAP信封和要发送的参数.

总结

以上是内存溢出为你收集整理的.net – 如何使用HttpWebRequest来调用接受byte []参数的Web服务 *** 作?全部内容,希望文章能够帮你解决.net – 如何使用HttpWebRequest来调用接受byte []参数的Web服务 *** 作?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1080810.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存