C# から [webmethod] を呼び出そうとしています。「文字列」パラメーターを受け取る単純な webmethod を呼び出すことができます。しかし、'byte[]' パラメータを受け取る Web メソッドがあります。呼び出しようとすると、「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());
}
これは完全に正常に機能します。今、私はこのように定義された別の webmethod を持っています
[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の内部エラーを与えます:(