我想在C#.net中创建一个可以通过drupal网站签名的表单。 Service 3.x模块已启用,Rest服务器正在我的网站上正常运行。 我唯一的问题是如何将用户名和密码序列化为json格式?
答案 0 :(得分:1)
最后一个解决方案:
我有一个Windows窗体,包含一个文本框,一个蒙版文本框和一个按钮(下面称为button3),
在按钮单击事件上,textbox和maskedtextbox内容将放置在使用此类构造的用户对象中:
class User
{
public string username;
public string password;
public string name;
public string number;
public string address;
public string email;
public User(string user, string pass, string name = "", string number = "", string address = "", string email = "")
{
this.username = user;
this.password = pass;
this.name = name;
this.number = number;
this.address = address;
this.email = email;
}
}
然后使用Json.net库
将此对象转换为json模型在其余部分中,它发出请求,传入的cookie将存储在cookieJar中,对于其他请求,您必须将此cookieContainer复制到您的请求cookieContainer中,您将保持登录状态。 这是代码的其余部分:
private void button3_Click(object sender, EventArgs e)
{
CookieContainer cookieJar = new CookieContainer();
User user = new User(textBox1.Text,maskedTextBox1.Text);
string url = "http://"your-web-address"/"your-rest-service"/?q="your-resurce"/user/login.json";
try
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/json";
request.Method = "POST";
request.CookieContainer = cookieJar;
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string query = JsonConvert.SerializeObject(user);
streamWriter.Write(query);
streamWriter.Flush();
streamWriter.Close();
}
var response = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
}
//Check to see if you're logged in
url = "http://web-address/rest-server/?q=resurce/system/connect.json";
var newRequest = (HttpWebRequest)WebRequest.Create(url);
newRequest.CookieContainer = cookieJar;
newRequest.ContentType = "application/json";
newRequest.Method = "POST";
var newResponse = (HttpWebResponse)newRequest.GetResponse();
using (var newStreamReader = new StreamReader(newResponse.GetResponseStream()))
{
var newResponseText = newStreamReader.ReadToEnd();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(), "Can't access server");
}
}
注意:第二个请求只是为了测试drupal是否了解我。
答案 1 :(得分:0)
你的问题非常模糊,但我可以告诉你登录的方法。
要使用服务登录Drupal站点,您可以启用用户资源和操作登录(和注销)。
使用以下内容登录,(根据您的设置进行更改)。
URL
- HOST: "http://www.yourserver.com"
- PATH: "/service-end-point/user/login"
METHOD
- POST CONTENT-TYPE: application/json
BODY
- RAW Input:
{ "username":"demo@gmail.com", "password":"demo" }