简体   繁体   English

C# 控制台应用程序 httpWebRequest

[英]C# Console Application httpWebRequest

Im trying to create a very simple c# console application to post some data to a web api.我试图创建一个非常简单的 c# 控制台应用程序来将一些数据发布到 web api。 However whatever I do I get an error on the response like:但是,无论我做什么,我都会在响应中收到错误,例如:

responseText "{\"info\":{\"status\":\"failed\",\"error\":{\"code\":1000,\"message\":\"Invalid argument from request\"}}}" string responseText "{\"info\":{\"status\":\"failed\",\"error\":{\"code\":1000,\"message\":\"来自请求的无效参数\ “}}}“ 细绳

The api http://www.detrack.com/api-documentation/ is looking for a post like api http://www.detrack.com/api-documentation/正在寻找类似的帖子

https://app.detrack.com/api/v1/deliveries/view/all.json?key=dab13cc0094620102d89f06c0e464b7de0782bb979258d3a&json= {"date":"2014-08-29"} https://app.detrack.com/api/v1/deliveries/view/all.json?key=dab13cc0094620102d89f06c0e464b7de0782bb979258d3a&json= {"date":"2014-08-29"}

I know using this in chrome advanced rest application extension returns a valid result.我知道在 chrome 高级休息应用程序扩展中使用它会返回有效结果。 But When I try the same via this console code.但是当我通过这个控制台代码尝试相同的时候。 I get an error!.我得到一个错误!

Here is the code I have in my console application.这是我在控制台应用程序中的代码。

using System;
using System.Net;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {

        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://app.detrack.com/api/v1/deliveries/view/all.json?key=dab13cc0094620102d89f06c0e464b7de0782bb979258d3a&");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "json={\"date\":\"2014-08-28\"}";
            Console.WriteLine(json);
            streamWriter.Write(json);
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var responseText = streamReader.ReadToEnd();
            Console.WriteLine(responseText);
            Console.ReadKey();

        }


    }
}
}

Any help/guidance would be really appreciated任何帮助/指导将不胜感激

brendan布伦丹

So I'm looking at this:所以我在看这个:

string json = "json={\"date\":\"2014-08-28\"}";

And according to the brief description on detrack that is not what you want.根据对脱轨的简要描述,这不是您想要的。 They're expecting valid json and that isn't.他们期待有效的 json,但事实并非如此。 Here's what you should be considering valid json:以下是您应该考虑的有效 json:

string json = "{\"date\":\"2014-08-28\"}";

Be warned I don't know about your escaping of quotes.请注意,我不知道您对引号的转义。 I would serialize that differently;我会以不同的方式序列化它; either a strongly typed class or an anonymous class.强类型类或匿名类。 Anon would look like this: Anon 看起来像这样:

string json = JsonConvert.DeserializeObject(new { date = "2014-08-28" });

Setting aside any concerns about time, timezones, utc, etc, that will serialize your structures correctly.抛开对时间、时区、UTC 等的任何担忧,这将正确地序列化您的结构。 Here's a scratchy program from linqpad:这是来自 linqpad 的一个粗糙的程序:

void Main()
{
    var json = Newtonsoft.Json.JsonConvert.SerializeObject(new { date = "2014-08-28"});
    Console.WriteLine(json);
}

>>> {"date":"2014-08-28"}

You can try the (untested!) code below.您可以尝试下面的(未经测试!)代码。

using System;
using System.Net;
using System.IO;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {

            var webAddr = "https://app.detrack.com/api/v1/deliveries/create.json";

            var httpWebRequest = (HttpWebRequest) WebRequest.Create(webAddr);

            httpWebRequest.ContentType = "application/x-www-form-urlencoded";

            httpWebRequest.Method = "POST";

            string postData = "key=dab13cc0094620102d89f06c0e464b7de0782bb979258d3a&json={""date"":""2014-08-28""}";

            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            httpWebRequest.ContentLength = byteArray.Length;

            using(var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))

            {

                streamWriter.Write(byteArray, 0, byteArray.Length);

                streamWriter.Flush();

            }

            var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();

            using(var streamReader = new StreamReader(httpResponse.GetResponseStream()))

            {

                var result = streamReader.ReadToEnd();

                MessageBox.Show(result);

            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM