REST服务在控制台应用程序中消耗

编程入门 行业动态 更新时间:2024-10-27 23:24:29
本文介绍了REST服务在控制台应用程序中消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我在控制台应用程序中使用REST WCF服务,并希望根据URL,BODY,METHOD显示所有信息。 REST服务URL: http:// localhost / TAExcelApps / TemplateService / GetTemplatesInfos [ ^ ] BODY: < ArrayOfTemplateInfo xmlns:i =www.w3/2001/XMLSchema-instance >< TemplateInfo>< UserID> 17< / UserID>< / TemplateInfo>< / ArrayOfTemplateInfo> 方法 :POST //////////////////////////////////// 我的编码低于哪个不起作用: 错误:远程服务器返回错误:(404)未找到。

Hi All, I am consuming a REST WCF service in console application and want to display all information according to URL,BODY,METHOD like below. REST Service URL: localhost/TAExcelApps/TemplateService/GetTemplatesInfos[^] BODY : <ArrayOfTemplateInfo xmlns:i="www.w3/2001/XMLSchema-instance"><TemplateInfo><UserID>17</UserID></TemplateInfo></ArrayOfTemplateInfo> Method :POST //////////////////////////////////// My coding is below which is not working: error:The remote server returned an error: (404) Not Found.

try { // Create URI string uri = "localhost/TAExcelApps/TemplateService/GetTemplatesInfos"; string gggg = "<arrayoftemplateinfo>"; string body1 = gggg+"<templateinfo>"+ "<userid>254</userid>"+ "</templateinfo>"+ "</arrayoftemplateinfo>"; string body=body1.ToString(); Uri address = new Uri(uri.ToString()); // Create the web request HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; // Set type to POST request.Method = "POST"; request.KeepAlive = true; request.Credentials = CredentialCache.DefaultCredentials; request.Headers.Add("Key1", "Value1"); // According to the requested content type, the request and response content type can be set request.ContentType = "text/xml"; request.Accept = "text/xml"; // HttpResponse httpResponse = client.execute(httpPost); byte[] byteData = UTF8Encoding.UTF8.GetBytes(body); //Set the content length in the request headers request.ContentLength = byteData.Length; //-- Write data --// using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } // Get response using (HttpWebResponse httpResponse = request.GetResponse() as HttpWebResponse) { Stream ResponseStream = null; ResponseStream = httpResponse.GetResponseStream(); int responseCode = (int)httpResponse.StatusCode; string responseBody = ((new StreamReader(ResponseStream)).ReadToEnd()); string contentType = request.ContentType; } } catch (WebException Ex) { if (Ex.Status == WebExceptionStatus.ProtocolError) { int StatusCode = (int)((HttpWebResponse)Ex.Response).StatusCode; Stream ResponseStream = null; ResponseStream = ((HttpWebResponse)Ex.Response).GetResponseStream(); string responseText = (new StreamReader(ResponseStream)).ReadToEnd(); if (StatusCode == 500) { // Do Something } else { // Do Something for other status codes } } else { throw (Ex); // Or check for other WebExceptionStatus } }

推荐答案

string postData =& lt; TemplateInfo xmlns:i ='http://www.w3/2001/XMLSchema-instance'& ; gt;中+& lt; UserID& gt; 254& lt; / UserID& gt; +& lt; / TemplateInfo& gt;; string postData = "&lt;TemplateInfo xmlns:i='www.w3/2001/XMLSchema-instance'&gt;" + "&lt;UserID&gt;254&lt;/UserID&gt;" + "&lt;/TemplateInfo&gt;"; byte[] byteArray = Encoding.UTF8.GetBytes(postData); //create an HTTP request to the URL that we need to invoke HttpWebRequest request = (HttpWebRequest)WebRequest.Create("172.19.18.126/TAExcelApps/TemplateService/GetTemplatesInfos"); request.ContentLength = byteArray.Length; request.ContentType = "application/xml"; //set the content type to XML request.Method = "POST"; //make an HTTP POST using (Stream dataStream = request.GetRequestStream()) { //initiate the request dataStream.Write(byteArray, 0, byteArray.Length); } // Get the response. using (HttpWebResponse httpResponse = request.GetResponse() as HttpWebResponse) { Stream ResponseStream = null; ResponseStream = httpResponse.GetResponseStream(); int responseCode = (int)httpResponse.StatusCode; string responseBody = ((new StreamReader(ResponseStream)).ReadToEnd()); Console.WriteLine(responseBody.ToString()); } Console.ReadLine();

更多推荐

REST服务在控制台应用程序中消耗

本文发布于:2023-11-14 13:19:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587317.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   应用程序   消耗   REST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!