Tea*_*ild 6 c# wcf httpclient httpwebrequest
我有一大堆代码使用HttpWebRequest和HttpWebResponse,但我想将其转换为使用HttpClient和HttpResponseMessage.
这是有效的代码块...
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(serviceReq);
request.Method = "POST";
request.ContentType = "text/xml";
string xml = @"<?xml version=""1.0""?><root><login><user>flibble</user>" +
@"<pwd></pwd></login></root>";
request.ContentLength = xml.Length;
using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
{
dataStream.Write(xml);
dataStream.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Run Code Online (Sandbox Code Playgroud)
这是我想要替换它的代码,只要我能使它工作.
/// <summary>
/// Validate the user credentials held as member variables
/// </summary>
/// <returns>True if the user credentials are valid, else false</returns>
public bool ValidateUser()
{
bool valid = false;
try
{
// Create the XML to be passed as the request
XElement root = BuildRequestXML("LOGON");
// Add the action to the service address
Uri serviceReq = new Uri(m_ServiceAddress + "?obj=LOGON");
// Create the client for the request to be sent from
using (HttpClient client = new HttpClient())
{
// Initalise a response object
HttpResponseMessage response = null;
// Create a content object for the request
HttpContent content = HttpContentExtensions.
CreateDataContract<XElement>(root);
// Make the request and retrieve the response
response = client.Post(serviceReq, content);
// Throw an exception if the response is not a 200 level response
response.EnsureStatusIsSuccessful();
// Retrieve the content of the response for processing
response.Content.LoadIntoBuffer();
// TODO: parse the response string for the required data
XElement retElement = response.Content.ReadAsXElement();
}
}
catch (Exception ex)
{
Log.WriteLine(Category.Serious,
"Unable to validate the Credentials", ex);
valid = false;
m_uid = string.Empty;
}
return valid;
}
Run Code Online (Sandbox Code Playgroud)
我认为问题是创建内容对象并且XML没有正确附加(也许).
我很想知道为什么一种方法不起作用而另一种方法有效,但我只是没有时间进行更多挖掘。{:o(
无论如何,这就是我发现的。
使用以下方式创建请求内容时会发生失败
HttpContent content = HttpContentExtensions.Create(root, Encoding.UTF8, "text/xml");
Run Code Online (Sandbox Code Playgroud)
但是当您创建这样的内容时它可以正常工作......
HttpContent content = HttpContent.Create(root.ToString(), Encoding.UTF8, "text/xml");
Run Code Online (Sandbox Code Playgroud)
最终的工作函数是这样的:
/// <summary>
/// Validate the user credentials held as member variables
/// </summary>
/// <returns>True if the user credentials are valid, else false</returns>
public bool ValidateUser()
{
bool valid = false;
try
{
// Create the XML to be passed as the request
XElement root = BuildRequestXML("LOGON");
// Add the action to the service address
Uri serviceReq = new Uri(m_ServiceAddress + "?obj=LOGON");
// Create the client for the request to be sent from
using (HttpClient client = new HttpClient())
{
// Initalise a response object
HttpResponseMessage response = null;
#if DEBUG
// Force the request to use fiddler
client.TransportSettings.Proxy = new WebProxy("127.0.0.1", 8888);
#endif
// Create a content object for the request
HttpContent content = HttpContent.Create(root.ToString(), Encoding.UTF8, "text/xml");
// Make the request and retrieve the response
response = client.Post(serviceReq, content);
// Throw an exception if the response is not a 200 level response
response.EnsureStatusIsSuccessful();
// Retrieve the content of the response for processing
response.Content.LoadIntoBuffer();
// TODO: parse the response string for the required data
XElement retElement = response.Content.ReadAsXElement();
}
}
catch (Exception ex)
{
Log.WriteLine(Category.Serious, "Unable to validate the user credentials", ex);
valid = false;
m_uid = string.Empty;
}
return valid;
}
Run Code Online (Sandbox Code Playgroud)
谢谢。
| 归档时间: |
|
| 查看次数: |
23262 次 |
| 最近记录: |