[英]How can i get NameValuePair data from post url in c#(MVC)
I am sending HttpPost request from android device by adding NameValuePair but not getting how to fetch the NameValuePair values in Controller. 我通过添加NameValuePair从android设备发送HttpPost请求,但未获取如何在Controller中获取NameValuePair值。
Here is the code for sending request:- 这是发送请求的代码:-
HttpPost request=new HttpPost(PostUrl);
HttpResponse response;
List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>(7);
nameValuePairs.add(new BasicNameValuePair("userName",userName));
nameValuePairs.add(new BasicNameValuePair("password",Password));
nameValuePairs.add(new BasicNameValuePair("deviceId",deviceId));
nameValuePairs.add(new BasicNameValuePair("subdomain",Subdomain));
nameValuePairs.add(new BasicNameValuePair("deviceType",DeviceType));
nameValuePairs.add(new BasicNameValuePair("uniqueId",UniqueId));
nameValuePairs.add(new BasicNameValuePair("appVersion",AppVersion));
request.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
response = client.execute(request);
this is the NameValuePair i am sending .. 这是我发送的NameValuePair ..
Now Here is the code(C#) of my Account Controller and Method login where i am posting these data. 现在,这是我发布这些数据的帐户控制器和方法登录名的代码(C#)。
public LoginResponse login( NameValueCollection post, String UserName, String password, String deviceId, String subdomain, String deviceType,String uniqueId,String appVersion)
{
String User=Convert.toString(post["userName"]);
}
Please tell me how should i get the NameValuePair in this method .if i send all parameters by appending to Post Url then its working fine but if i am sending it through NameValuePair then the value of User is always null 请告诉我如何使用此方法获取NameValuePair。如果我通过附加到Post Url发送所有参数,则其工作正常,但是如果我通过NameValuePair发送,则User的值始终为null
Try this: 尝试这个:
public LoginResponse login(NameValueCollection post)
{
string test1 = post.Get("userName");
string test2 = post.Get("password");
}
Also, if this is MVC pick a standard result (in this case, ContentResult returns a string): 另外,如果这是MVC,请选择一个标准结果(在这种情况下,ContentResult返回一个字符串):
public ContentResult login(NameValueCollection post)
{
LoginResponse loginResponse = //some method call...
return Content("login", loginResponse)
}
Let me know if it works. 让我知道它是否有效。 If not I have a couple of other suggestions for you.
如果没有,我还有其他建议给您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.