问题描述
大家好,
我在控制台应用程序中使用REST WCF服务,并希望根据URL,BODY,METHOD显示所有信息。
REST服务URL:
[]
BODY:
< ArrayOfTemplateInfo xmlns:i =http://www.w3.org/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:
http://localhost/TAExcelApps/TemplateService/GetTemplatesInfos[^]
BODY :
<ArrayOfTemplateInfo xmlns:i="http://www.w3.org/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 = "http://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
}
}
推荐答案
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
//create an HTTP request to the URL that we need to invoke
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://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服务在控制台应用程序中消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!