C#如何使用剩余的Web服务

时间:2018-09-24 14:14:09

标签: c# rest http webclient fiddler

当我尝试在C#中使用Rest Web服务时遇到问题。

当我通过Fiddler尝试时,效果很好。 当我通过HTML / Ajax尝试时,它也可以正常运行。

当我尝试通过C#(控制台应用程序)时,出现错误。

此图像在提琴手中捕获。这是我通过ajax尝试得到的结果 enter image description here

此图像也被提琴手捕获。这是我通过C#尝试得到的结果 enter image description here

如您所见,JSON字段为空。

这是我的C#代码

            string json = JsonConvert.SerializeObject(abc);

        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("MyURL");  //==> I am filling it correctly
        client.DefaultRequestHeaders.Accept.Add(
           new MediaTypeWithQualityHeaderValue("application/json"));

        var response = client.PostAsJsonAsync("MyMethod", json).Result; //==> I am filling my method correctly

但是我尝试了其他几个,总是遇到同样的问题。 (下面的代码是我尝试过的)

var requisicaoWeb = WebRequest.CreateHttp("MyURL");
        requisicaoWeb.Method = "POST";
        requisicaoWeb.ContentType = "application/json";
        requisicaoWeb.ContentLength = dados.Length;
        requisicaoWeb.UserAgent = "Console app";
        requisicaoWeb.Accept = "Accept:application/json,text/javascript,*/*;q=0.01";


        //precisamos escrever os dados post para o stream
        using (var stream = requisicaoWeb.GetRequestStream())
        {
            stream.Write(MyJson, 0, dados.Length);
            stream.Close();
        }
        //ler e exibir a resposta
        using (var resposta = requisicaoWeb.GetResponse())
        {
            var streamDados = resposta.GetResponseStream();
            StreamReader reader = new StreamReader(streamDados);
            object objResponse = reader.ReadToEnd();
            var post = objResponse.ToString();//JsonConvert.DeserializeObject<Post>(objResponse.ToString());
            streamDados.Close();
            resposta.Close();
        }

我在C#中尝试的所有操作,Fiddler上的JSON字段始终为空,并且“语法视图”描述始终为“请求无效”。

2 个答案:

答案 0 :(得分:1)

尝试一下;

public static string HttpPost(string URI, string Parameters)
{
    System.Net.WebRequest req = System.Net.WebRequest.Create(URI);

    req.ContentType = "application/json; charset=utf-8";
    req.Method = "POST";
    req.Timeout = 600000;
    byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
    req.ContentLength = bytes.Length;
    System.IO.Stream os = req.GetRequestStream();
    os.Write(bytes, 0, bytes.Length);
    os.Close();
    System.Net.WebResponse resp = req.GetResponse();
    if (resp == null)
        return null;
    System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
    return sr.ReadToEnd().Trim();
}

答案 1 :(得分:0)

我已经弄清楚了。

如果其他人有相同的问题,这是答案

 string json = JsonConvert.SerializeObject(abc);

        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("MyURL");  //==> I am filling it correctly
        client.DefaultRequestHeaders.Accept.Add(
           new MediaTypeWithQualityHeaderValue("application/json"));

        var stringContent = new StringContent(JsonConvert.SerializeObject(abc), Encoding.UTF8, "application/json"); 

        var response = client.PostAsync("MyURL", stringContent).Result; //==> I am filling my method correctly