本文介绍了HTTP发布到Web服务的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我无法弄清楚如何发布到网络服务。我的控制台应用程序(客户端)中有以下代码。 // 使用可以接收帖子的URL创建请求。 WebRequest request = WebRequest.Create( http://tempuri.org/HelloWorld ); // 将请求的Method属性设置为POST。 请求。方法= POST; // 创建POST数据并将其转换为字节数组。 string postData = 这是一个将此字符串发布到的测试一个Web服务器。; byte [] byteArray = Encoding.UTF8.GetBytes(postData); // 设置WebRequest的ContentType属性。 request.ContentType = application / x-www-form-urlencoded; // 设置WebRequest的ContentLength属性。 request.ContentLength = byteArray.Length; // 获取请求流。 流dataStream = request.GetRequestStream( ); // 将数据写入请求流。 dataStream.Write( byteArray, 0 ,byteArray.Length); // 关闭Stream对象。 dataStream.Close(); // 获取回复。 WebResponse response = request.GetResponse() ; // 显示状态。 Console.WriteLine(((HttpWebResponse)响应).StatusDescription); // 获取包含服务器返回内容的流。 dataStream = response.GetResponseStream(); // 使用StreamReader打开流以便于访问。 StreamReader阅读器= new StreamReader(dataStream); // 阅读内容。 string responseFromServer = reader.ReadToEnd(); // 显示内容。 Console.WriteLine(responseFromServer); // 清理流。 reader.Close(); dataStream.Close(); response.Close(); 它似乎运行正常。这是来自我的网络服务的代码。 命名空间 WebService1 { /// < 摘要 > /// Service1的摘要说明 /// < / summary > ; [WebService(Namespace = http://tempuri.org /)] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem( false )] // 要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。 // [System.Web.Script.Services.ScriptService] public class Service1:System.Web.Services.WebService { [WebMethod] public string HelloWorld( string postData) { return postData; } } } 这似乎也运行良好。但是,当我从客户端发布时,我没有看到Web服务上的信息。我对Web服务知之甚少,所以它可能很简单。我能够调用hello world方法并输入一个字符串值,然后查看正确的xml数据,但我不确定在哪里或如何查看我从客户端发布的数据。请帮助并记住,我是一个完整的新手。在此先感谢!解决方案 您的代码无法工作的许多可能性。 1) WebService.Namespace - 看看下面的链接,了解它的含义。它不是您服务的网址。 http://msdn.microsoft.com/en-us/library/system.web.services.webserviceattribute.namespace.aspx http://social.msdn.microsoft.com/forums/ en-US / asmxandxml / thread / ad85151b-868e-4c30-9bfc-a0c0affb6827 / 2)首先弄清楚,你的网址是什么服务? 3)写一个简单的 GET 方法,看看你是否可以在浏览器中访问你的网址。 您在这里使用的基本概念是 REST 。看看下面的链接,了解什么是 REST ? https: //en.wikipedia.org/wiki/Representational_state_transfer 如果你想纯粹基于 REST 获取/发布数据我建议你学习WCF REST服务。 http://msdn.microsoft.com/en-us/magazine/dd315413.aspx I am having trouble figuring out how posting to a web service works. I have the following code inside my console app(client).// Create a request using a URL that can receive a post. WebRequest request = WebRequest.Create("http://tempuri.org/HelloWorld"); // Set the Method property of the request to POST. request.Method = "POST"; // Create POST data and convert it to a byte array. string postData = "This is a test that posts this string to a Web server."; byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Set the ContentType property of the WebRequest. request.ContentType = "application/x-www-form-urlencoded"; // Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length; // Get the request stream. Stream dataStream = request.GetRequestStream(); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); // Close the Stream object. dataStream.Close(); // Get the response. WebResponse response = request.GetResponse(); // Display the status. Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); // Display the content. Console.WriteLine(responseFromServer); // Clean up the streams. reader.Close(); dataStream.Close(); response.Close();And it seems to be running fine. And here is my code from my web service.namespace WebService1{ /// <summary> /// Summary description for Service1 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class Service1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld(string postData) { return postData; } }}Which also seems to be running fine. But when I post from the client i am not seeing the information on the web service. I do not know much about web services so it could be something simple. I am able to invoke the hello world method and enter a string value and then see the correct xml data but i''m not really sure where or how to see the data that i''m posting from the client. Please help and keep in mind that i''m a complete newbie. Thanks in Advance! 解决方案 Many possibilities why your code is not working.1) WebService.Namespace - Have a look at below link to understand what does it means. Its not the url of your service.http://msdn.microsoft.com/en-us/library/system.web.services.webserviceattribute.namespace.aspxhttp://social.msdn.microsoft.com/forums/en-US/asmxandxml/thread/ad85151b-868e-4c30-9bfc-a0c0affb6827/2) Figure out first, what is an url of your service?3) Write a simple GET method and see if you can access your url in browser.Fundamental concept you are using here is REST. Have a look at below link to understand, What is REST?https://en.wikipedia.org/wiki/Representational_state_transferIf you want to GET/POST data purely REST based then I recommend you to learn WCF REST Service.http://msdn.microsoft.com/en-us/magazine/dd315413.aspx 这篇关于HTTP发布到Web服务的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 22:36