我正在尝试创建一个Web服务。我成功地将HttpClient请求发送到Web服务并获得响应。
我想要什么?
我正在发送一些带有POSTA请求的HttpHeaders,如userAgent或任何CustomHeader。我希望在webservice方法中阅读的Header。我不知道如何获得Header列表?
我在C#中创建了webservice。
public class Service1 :IService1{
public string putData(Stream data)
{
string response = string.Empty;
try
{
HttpContext ctx = HttpContext.Current;
string headerValue = ctx.Request.Headers["tej"];
StreamReader reader = new StreamReader(data);
string xmlString = reader.ReadToEnd();
StringReader sr = new StringReader(xmlString);
MySqlCommand cmd = new MySqlCommand();
DataSet ds = new DataSet();
ds.ReadXml(sr);
//my logic here....
return "Passed";
}
catch (Exception ex)
{
return "Failed";
}
}
}
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "putdata")]
string putData(Stream sDatabase);
}
答案 0 :(得分:0)
请尝试使用WebOperationContext.Current.IncomingRequest对象。
public class Service1 :IService1
{
public string putData(Stream data)
{
try
{
//reading headers
IncomingWebRequestContext request = WebOperationContext.Current.IncomingRequest;
WebHeaderCollection headers = request.Headers;
foreach (string headerName in headers.AllKeys)
{
Console.WriteLine(headerName + ": " + headers[headerName]);
}
//---- rest of the code
}
catch (Exception ex)
{
return "Failed";
}
}
}