she*_*ill 5 .net web-services asmx httpwebrequest
我试图从C#调用[webmethod].我可以调用带有'string'参数的简单webmethod.但我有一个webmethod接受'byte []'参数.当我尝试调用它时,我遇到了"500内部服务器错误".这是我正在做的一些例子.
让我们说我的方法是这样的
[WebMethod]
public string TestMethod(string a)
{
return a;
}
Run Code Online (Sandbox Code Playgroud)
我在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());
}
Run Code Online (Sandbox Code Playgroud)
这完全没问题.现在我有另一个像这样定义的web方法
[WebMethod]
public string UploadFile(byte[] data)
Run Code Online (Sandbox Code Playgroud)
我试着这样称呼它
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);
Run Code Online (Sandbox Code Playgroud)
但这给了我一个500内部错误:(
小智 5
有可能,我自己做了
首先是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");
Run Code Online (Sandbox Code Playgroud)
然后是请求部分
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();
Run Code Online (Sandbox Code Playgroud)
现在,您可以从响应中获取传入的流
请记住根据webservice中页面详细信息给出的描述(即http://myweb.com/WS/MyWS.asmx?op = Validation)调整SOAP信封和要发送的参数.