C# 错误 401 - 无效的用户名或密码
C# Error 401 - Invalid username or password
我正在尝试向 API 发出请求。
当我使用邮递员发出请求时,它有效。但在 c# 中,它没有,它 returns 错误 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
我正在尝试向 API 发出请求。 当我使用邮递员发出请求时,它有效。但在 c# 中,它没有,它 returns 错误 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