我正在使用ASP.NET,我通过动词GET接收查询,该URL对于此应用程序是特殊的。
代码有效,但是如果用户发送的密码无效,那么http 200 Ok回答我,并在邮件正文中#34;失败用户或密码"。
namespace WebApp_dev.Controllers
{
public class ValuesController : ApiController
{
[HttpGet]
public string Save(string point)
{
// in point variable you will get part between /dev and /save
// for /dev/point/save?name=125.25 url it will be "point"
// get parameters that follow by ? mark in url
var queryParams = GetQueryParams(Request);
string url_completa = Request.RequestUri.AbsolutePath;
int _indexPto = url_completa.IndexOf("/dev/");
url_completa=url_completa.Remove(0, (_indexPto + 5));
// _indexPto = url_completa.IndexOf("/");
url_completa = url_completa.Remove(url_completa.IndexOf("/"), (url_completa.Length - url_completa.IndexOf("/"))); // tengo el nombre del punto en url_completa
//--------------------------------------------------
string url_query = Request.RequestUri.Query;
_indexPto = url_query.IndexOf("&u=");
url_query = url_query.Remove(0, _indexPto+1);
string[] _User_pasw = url_query.Split(char.Parse("&"));//con esto queda la cadena separada el usuario y contraseña
string _usuario, _pasword;
_usuario = _User_pasw[0].Split(char.Parse("="))[1];
_pasword = _User_pasw[1].Split(char.Parse("="))[1];
bool _usuarioValido = UsuarioValido(_usuario, _pasword);
if (_usuarioValido == false)
{
return "Fail user or pasword";
}
// loop through all of them
foreach (var pair in queryParams)
{
string paramName = pair.Key; // for /dev/point/save?name=125.25 will be "name"
string paramvalue = queryParams[pair.Key]; // for /dev/point/save?name=125.25 will be 125.25
AlmacenarValor(url_completa.ToString(), paramvalue, paramName);
}
return "OK: " + url_completa.ToString();
}
private bool UsuarioValido(string _usuario, string _pasword)
{
MonitoreoEntities _context = new MonitoreoEntities();
PuntoDeMedicion _pdm = _context.PuntoDeMedicion.FirstOrDefault(a => a.Nombre == _usuario && a.Contrasena == _pasword);
if (_pdm == null)
return false;
else
return true;
}
private Dictionary<string, string> GetQueryParams(HttpRequestMessage request)
{
return request.GetQueryNameValuePairs()
.ToDictionary(kv => kv.Key, kv => kv.Value,
StringComparer.OrdinalIgnoreCase);
}
public static void AlmacenarValor(string _dispositivo, string _valor, string _parametro)
{
MonitoreoEntities _context = new MonitoreoEntities();
PuntoDeMedicion _puntoDeMedicion = _context.PuntoDeMedicion.Include("EntradaSalida").Where(a => a.Nombre == _dispositivo).FirstOrDefault();
foreach (EntradaSalida _entradasalida in _puntoDeMedicion.EntradaSalida)
{
if (_entradasalida.Etiqueta == _parametro)
{
Registro _registro = new Registro()
{
EntradaSalidaId = _entradasalida.Id,
FechaHoraCreacion = DateTime.Now,
FechaHoraRegistro = DateTime.Now,
PuntoDeMedicionId = _puntoDeMedicion.Id,
Valor = decimal.Parse(_valor)
};
_context.Registro.Add(_registro);
_context.SaveChanges();
break;
}
}
}
}
}
为Google Chrome使用高级REST客户端:
响应用户确定(密码好)
200 OK Show explanation Loading time: 8723
Request headers
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36
Content-Type: text/plain; charset=utf-8
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es-ES,es;q=0.8,en;q=0.6
Response headers
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcUHJveWVjdG8gdGVsZW1ldHJpYVxTZXJ2ZXJcV2ViQXBwX2RldlxkZXZccHJ1ZWJhMTIzXHNhdmU=?=
X-Powered-By: ASP.NET
Date: Mon, 22 Sep 2014 22:23:01 GMT
Content-Length: 28
Raw
JSON
Response
Word wrap Copy to clipboard Save as file
&#34;好的:list123&#34;
响应用户密码无效
200 OK Show explanation Loading time: 1415
Request headers
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36
Content-Type: text/plain; charset=utf-8
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es-ES,es;q=0.8,en;q=0.6
Response headers
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcUHJveWVjdG8gdGVsZW1ldHJpYVxTZXJ2ZXJcV2ViQXBwX2RldlxkZXZccHJ1ZWJhMTIzXHNhdmU=?=
X-Powered-By: ASP.NET
Date: Mon, 22 Sep 2014 22:30:14 GMT
Content-Length: 18
Raw
JSON
Response
Copy to clipboard Save as file
&#34;用户或密码失败&#34;
以这种方式编程,它正常工作,如果用户或密码不正确且http 200 Ok正确,我想更改此消息并发送HTTP 409 Conflict。
非常感谢您的回复
我得到一系列错误,是另一个上下文中的变量:
*
_usuarioValido
url_completa
*
我可以将它从一个上下文传递到另一个上下文。 代码保持如下:
public class ValuesController : ApiController
{
[HttpGet]
public string Save(string point)
{
// in point variable you will get part between /dev and /save
// for /dev/point/save?name=125.25 url it will be "point"
// get parameters that follow by ? mark in url
var queryParams = GetQueryParams(Request);
string url_completa = Request.RequestUri.AbsolutePath;
// Busco el nombre del punto de medicion-----------
int _indexPto = url_completa.IndexOf("/dev/");
url_completa=url_completa.Remove(0, (_indexPto + 5));
// _indexPto = url_completa.IndexOf("/");
url_completa = url_completa.Remove(url_completa.IndexOf("/"), (url_completa.Length - url_completa.IndexOf("/"))); // tengo el nombre del punto en url_completa
//--------------------------------------------------
string url_query = Request.RequestUri.Query;
_indexPto = url_query.IndexOf("&u=");
url_query = url_query.Remove(0, _indexPto+1);
string[] _User_pasw = url_query.Split(char.Parse("&"));//con esto queda la cadena separada el usuario y contraseña
string _usuario, _pasword;
_usuario = _User_pasw[0].Split(char.Parse("="))[1];
_pasword = _User_pasw[1].Split(char.Parse("="))[1];
bool _usuarioValido = UsuarioValido(_usuario, _pasword);
if (_usuarioValido == false)
{
return "Usuario invalido";
}
// loop through all of them
foreach (var pair in queryParams)
{
string paramName = pair.Key; // for /dev/point/save?name=125.25 will be "name"
string paramvalue = queryParams[pair.Key]; // for /dev/point/save?name=125.25 will be 125.25
AlmacenarValor(url_completa.ToString(), paramvalue, paramName);
}
return "SUCCESS OK de: " + url_completa.ToString()+"\n";// mando el ok, tiene que llevar el fin de linea.
}
[HttpGet]
[ResponseType(typeof(String))]
public HttpResponseMessage Save(HttpRequestMessage request, string point)
{
if (_usuarioValido == false)
{
return request.CreateResponse(HttpStatusCode.Conflict, "Fail user or password");
}
return request.CreateResponse(HttpStatusCode.OK, url_completa.ToString());
}
答案 0 :(得分:0)
我们试试这个:
[HttpGet]
[ResponseType(typeof(String))]
public HttpResponseMessage Save(HttpRequestMessage request, string point)
{
.......
if (_usuarioValido == false)
{
return request.CreateResponse(HttpStatusCode.Conflict, "Fail user or password");
}
.......
return request.CreateResponse(HttpStatusCode.OK, url_completa.ToString());
}
我还没有测试过上面的代码,但大致有这个概念。
希望它有所帮助。
谢谢!