gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:59:56 26 4
gpt4 key购买 nike

我正在尝试从 C# 调用 [webmethod]。我可以调用接受“字符串”参数的简单 web 方法。但是我有一个 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());
}

这工作得很好。现在我有另一个像这样定义的 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 内部错误:(

最佳答案

有可能,我自己做过

首先是 Header 设置,如果您的 webservice 可以通过 web 执行并发送参数,则可以获得此设置。我使用 Chrome 的开发人员工具。简单的方法是查看网络服务的描述(即 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 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();

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

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

关于.net - 如何使用 HttpWebRequest 调用接受 byte[] 参数的 Web 服务操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/703869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com