我有一个具有用户名和密码验证模式的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());
}