![.net – 如何使用HttpWebRequest来调用接受byte []参数的Web服务 *** 作?,第1张 .net – 如何使用HttpWebRequest来调用接受byte []参数的Web服务 *** 作?,第1张](/aiimages/.net+%E2%80%93+%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8HttpWebRequest%E6%9D%A5%E8%B0%83%E7%94%A8%E6%8E%A5%E5%8F%97byte+%5B%5D%E5%8F%82%E6%95%B0%E7%9A%84Web%E6%9C%8D%E5%8A%A1%E6%93%8D%E4%BD%9C%EF%BC%9F.png)
让我们说我的方法是这样的
[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服务 *** 作?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)