当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《如何使用HttpWebRequest来调用接受byte[]参数的Web服务操作?》经验,为你挑选了1个好方法。

我试图从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, 0, sendBytes.Length);

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



1> 小智..:

有可能,我自己做了

首先是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 = ""+
        "@Data";
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(); 

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

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

推荐阅读
凹凸曼00威威_694
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有