C#错误401-用户名或密码无效
本文关键字:密码 无效 用户 错误 401- | 更新日期: 2023-09-27 17:59:36
我正在尝试向API发出请求。当我用邮递员提出请求时,它是有效的。但在c#中,它没有,它返回错误401。我检查了登录名和密码是否正确。有人能帮我吗?
代码:
public JsonResult Test()
{
var document = new HtmlAgilityPack.HtmlDocument();
var password = CalculateMD5Hash("******");
var httpRequest =(HttpWebRequest)WebRequest.Create(string.Format("https://api.akna.com.br/emkt/int/integracao.php?User={0}&Pass={1}&XML={2}",
"mail@mail.com",
password,
@"<main>
<emkt trans='19.10'>
<datainicial>2016-07-01 10:00:00</datainicial>
<datafinal>2016-07-02 10:00:00</datafinal>
</emkt>
</main>"));
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
try
{
var httpWebResponse = (HttpWebResponse)httpRequest.GetResponse();
var responseStream = httpWebResponse.GetResponseStream();
if (responseStream != null)
{
var sr = new StreamReader(responseStream, Encoding.Default);
document.LoadHtml(sr.ReadToEnd());
}
return Json(new { StatusCode = 200 }, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json(new { StatusCode = 500, Erro = ex.Message }, JsonRequestBehavior.AllowGet);
}
}
问题在于参数的编码。它解决了这个问题:http://rest.elkstein.org/2008/02/using-rest-in-c-sharp.html