我有一个具有用户名和密码身份验证模式的 Web 服务 URL。我必须先传递用户名和密码,如果我通过了身份验证,我可以将文本或 XML 文件上传到服务器上。我正在寻找一个 C# 代码来执行相同的过程,但我找不到它。
任何建议将不胜感激。
我正在使用以下代码-
if (!string.IsNullOrEmpty(txtfile))
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.KeepAlive = false;
request.SendChunked = true;
request.AllowAutoRedirect = true;
request.Method = "Post";
request.ContentType = "text/xml";
request.Credentials = new NetworkCredential(userName, password);
var encoder = new UTF8Encoding();
var data = encoder.GetBytes(txtfile);
request.ContentLength = data.Length;
var reqStream = request.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
WebResponse response = null;
response = request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
}