Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C Http WebRequest JSON_C#_Http Headers_Http Post_Httpwebrequest_Webrequest - Fatal编程技术网

C# C Http WebRequest JSON

C# C Http WebRequest JSON,c#,http-headers,http-post,httpwebrequest,webrequest,C#,Http Headers,Http Post,Httpwebrequest,Webrequest,我不熟悉WebRequests,我尝试了以下方法: string json = @"{""Gebruikersnaam"":""user"",""Wachtwoord"":""pass"",""IngelogdBlijven"":true}"; var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://pontessg.magister.net/api/sessies"); httpWebReq

我不熟悉WebRequests,我尝试了以下方法:

string json = @"{""Gebruikersnaam"":""user"",""Wachtwoord"":""pass"",""IngelogdBlijven"":true}";
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://pontessg.magister.net/api/sessies");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.ContentLength = json.Length;
        Console.WriteLine(json.Length);
        Console.WriteLine(json);
        Console.Read();
        httpWebRequest.Method = "POST";

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


            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            Console.WriteLine(result);
        }
我的代码不断返回错误400,我真的不知道为什么我认为我忘记了什么,但我不知道是什么

标题

Request URL:https://pontessg.magister.net/api/sessies
Request Method:POST
Status Code:200 OK
Remote Address:[...]
Response Headers
view parsed
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 76
Content-Type: application/json; charset=utf-8
Expires: -1
Date: Sat, 25 Feb 2017 21:45:33 GMT
X-Frame-Options: DENY
Strict-Transport-Security: max-age=[...]
Request Headers
view parsed
POST /api/sessies HTTP/1.1
Host: pontessg.magister.net
Connection: keep-alive
Content-Length: 88
Accept: application/json, text/plain, */*
X-API-Client-ID: 12D8
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Origin: https://pontessg.magister.net
Content-Type: application/json;charset=UTF-8
DNT: 1
Referer: https://pontessg.magister.net/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8
Cookie: SESSION_ID=[...]; M6UserName=user; username=pass;     Magister.UserName=user; [...]
Request Payload

{"Gebruikersnaam":"user","Wachtwoord":"pass","IngelogdBlijven":true}
当我在浏览器的控制台中尝试下面的JS代码时,它返回200 OK

$.ajax({
url: "https://pontessg.magister.net/api/sessies",
type: "POST",
data: "{\"Gebruikersnaam\":\"user\",\"Wachtwoord\":\"pass\",\"IngelogdBlijven\":true}",
contentType: "application/json;charset=utf-8",
success: action_Succeeded,
error: action_Failed
});

function action_Succeeded(r) {
console.log("succes");
}

function action_Failed(r1, r2, r3) {
alert("fail");
}

如果有人能帮我,我希望

我想你需要向服务器提供某种身份验证,比如使用Cookie头。您的代码在浏览器中工作的原因可能是因为您已经登录到该网站,并且具有随请求一起发送的有效cookie。当从.NET应用程序发出请求时,您还需要提供此类cookie。Gebruikersnaam是userame,Wachtwoord是password,但是如何添加cookie容器?