![](/img/trans.png)
[英]Webservice+consumer c# error Server was unable to process request. ---> The connection is already open
[英]C# WebService: server can't process the request
我正在嘗試向 Visual Studio 2022 中使用ASP.NET Web 應用程序 (.NET Framwork)創建的 WebService 發送 JSON 消息。但是,當我嘗試使用 POST 方法發送消息時,我收到了下一個異常。 但是,如果我嘗試向JSONPlaceholder 免費發送假休息 api ,它會正常工作。
Hello, World!
Send msg:
Hello_World.
=====
{"Transmission":"Hello_World"}
=====
Response:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="es">System.Web.Services.Protocols.SoapException: El servidor no puede procesar la solicitud. ---> System.Xml.XmlException: Los datos del nivel de raíz no son válidos. línea 1, posición 1.
en System.Xml.XmlTextReaderImpl.Throw(Exception e)
en System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
en System.Xml.XmlTextReaderImpl.ParseDocumentContent()
en System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read()
en System.Xml.XmlReader.MoveToContent()
en System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement()
en System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()
en System.Web.Services.Protocols.SoapServerProtocol.Initialize()
en System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)
--- Fin del seguimiento de la pila de la excepción interna ---</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>
有誰知道如何在 Web 服務 ASMX 中正確接受 POST 請求?
我的客戶端程序是:
private static async Task Main (string[] args) {
Console.WriteLine("Hello, World!");
try {
/*
* Check web service created locally.
* string wsUrl = "https://localhost:44373/SoapDemo.asmx";
* string wsUrl = "https://jsonplaceholder.typicode.com/posts";
*/
string wsUrl = "https://localhost:44373/SoapDemo.asmx";
// Send a small string.
string msg = "Hello_World";
Console.WriteLine("Send msg:\n{0}.", msg);
// Create connection to Web Service.
var client = new HttpClient();
// Prepare the data to send to WS.
Post postMsg = new Post();
postMsg.Transmission = msg;
var data = JsonSerializer.Serialize<Post>(postMsg);
// Finally send the msg to the WebService.
Console.WriteLine("=====\n" + data + "\n=====");
HttpContent content = new StringContent(data, System.Text.Encoding.UTF8, "application/json");
// Recive a response from the Web Service server.
var httpResponse = await client.PostAsync(wsUrl, content);
// if (httpResponse.IsSuccessStatusCode) {
var result = await httpResponse.Content.ReadAsStringAsync();
Console.WriteLine("Response:\n{0}", result);
// var postResult = JsonSerializer.Deserialize<Post>(result);
// Console.WriteLine("Response:\n{0}", postResult);
// }
}
catch (Exception ex) {
Console.WriteLine("Transmission error:\n{0}", ex.ToString());
}
}
還有我的 Web 服務 ASMX 函數:
[WebMethod]
// [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public bool Transmission () {
ABCObject ObjectName = null;
string contentType = HttpContext.Current.Request.ContentType;
if (false == contentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase)) {
return false;
}
using (System.IO.Stream stream = HttpContext.Current.Request.InputStream)
using (System.IO.StreamReader reader = new System.IO.StreamReader(stream)) {
stream.Seek(0, System.IO.SeekOrigin.Begin);
string bodyText = reader.ReadToEnd();
bodyText = bodyText == "" ? "{}" : bodyText;
var json = Newtonsoft.Json.Linq.JObject.Parse(bodyText);
ObjectName = Newtonsoft.Json.JsonConvert.DeserializeObject<ABCObject>(json.ToString());
}
return true;
}
我從Accepting a json request with a Webservice (asmx)中獲取了這個 ASMX 解決方案。
有人可以幫我嗎? 非常感謝你的幫助
)
正如@Panagiotis Kanavos 正確地說的那樣,這已經過時了。 但是,我發現了一種更舒適的方式來與 ASMX 服務進行通信。
YourServiceSoapClient ws = new YourServiceSoapClient(YourServiceSoapClient.EndPointConfiguration.YourServiceSoap);
// Call the ASMX service functions.
string wsHelloWorld = ws.HelloWorld();
Console.WriteLine(wsHelloWorld);
我附上了一些鏈接,以防有人想做一些研究。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.