0

我们有一个使用 Visual Studio 的 C# 项目任务,我们的讲师给了我们这个示例代码,尝试使用 Json 字符串连接来构建某种电子销售点系统。

不幸的是,我不知道他希望我们如何与该服务器进行通信,因为我们已经编写了大约 2 个月的代码。他给了我们每个人一个访问令牌和 ID,我们必须先将其解析到服务器才能访问,但我不知道它采用什么格式。为了论证,假设 id = "epos3" 和访问令牌 = 123456789

private void button1_Click(object sender, EventArgs e)
    {
        var request =System.Net.WebRequest.Create("http://ap.hbwd.in//item/{2812748393}") as System.Net.HttpWebRequest;
         request.Method = "GET";
        request.ContentLength = 0;
        string responseContent;
        using (var response = request.GetResponse() as System.Net.HttpWebResponse)
        {
            using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
            {
                responseContent = reader.ReadToEnd();
            }
        }//end


    }

    private void button2_Click(object sender, EventArgs e)
    {
        // Code for request with JSON text body. Note JSON is just a string here.
        var request = System.Net.WebRequest.Create("http://ap.hbwd.in/Products") as System.Net.HttpWebRequest;
        request.Method = "POST";
        request.ContentType = "application/json";
        request.Headers.Add("Content-Type", "application/json");
        using (var writer = new System.IO.StreamWriter(request.GetRequestStream()))
        {
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes("{\n    \"store_id\": \"72932\",\n    \"access_token\": \"A733BE122\",\n    \"cashier_id\": \"12\",\n    \"items\": [[\"1\", 1], [\"4\", 2]],\n    \"payment\": [[\"cc\", 27.35], [\"ch\", 10.00]],\n}");
            request.ContentLength = byteArray.Length;
            writer.Write(byteArray);
            writer.Close();
        }
        string responseContent;
        using (var response = request.GetResponse() as System.Net.HttpWebResponse)
        {
            using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
            {
                responseContent = reader.ReadToEnd();
            }
        }//end
4

1 回答 1

0
private void button1_Click(object sender, EventArgs e)
    {
        var request = System.Net.WebRequest.Create("http://ap.hbwd.in/item/2812748393") as System.Net.HttpWebRequest;
        request.Method = "GET";
        request.ContentLength = 0;
        string responseContent;
        using (var response = request.GetResponse() as System.Net.HttpWebResponse)
        {
            using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
            {
                responseContent = reader.ReadToEnd();
                textBox1.Text = responseContent;
            }
        }//end
于 2013-10-14T18:39:21.557 回答