使用C#.net和json远程登录drupal网站 [英] Signing into drupal website remotly using C#.net and json

查看:345
本文介绍了使用C#.net和json远程登录drupal网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C#.net中创建一个可以通过drupal网站登录的表单。
服务3.x模块已启用,Rest服务器正确运行在我的网站上。
我唯一的问题是如何将用户名和密码序列化为json格式?

解决方案

最后一个解决方案: p>

我有一个Windows窗体,包含一个文本框,一个蒙版文本框和一个按钮(下面称为button3),



在按钮单击事件上,文本框和maskedtextbox内容将放置在使用此类构建的用户对象中:

 用户
{
public string username;
public string password;
public string name;
public string number;
公共字符串地址;
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



其余的请求传入cookie将存储在cookieJar和休息请求您必须将此CookieContainer复制到您的请求cookieContainer中,您将保持登录。
这是代码的其余部分:

  private void button3_Click(object sender,EventArgs e)
{
CookieContainer cookieJar = new CookieContainer();
用户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;

使用(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();
}

//检查您是否登录
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 not访问服务器);
}
}

注意:第二个请求只是测试drupal是否知道我


I want to create a form in C#.net which can sign me in through a drupal website. Service 3.x module is enabled and Rest server is running on my website correctly. My only problem is how to serialize the username and password into json format?

解决方案

Finally a solution :

I've got a windows form , contains a textbox , a masked-textbox and a button (called button3 below) ,

on button click event , the textbox and maskedtextbox contents will placed in the user object which is constructed using this class :

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;
    }
}

then converted this object into json model using Json.net library

in the rest it makes a request the incoming cookie will store in cookieJar and for rest Requests you must copy this cookieContainer into your request cookieContainer and you will stay logged in. this is the rest of Code :

        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");
        }
    }

note: Second request is just to test if drupal knows me.

这篇关于使用C#.net和json远程登录drupal网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆