热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使带有请求标头和正文的HttpClientRequestVisualC#

如何解决《使带有请求标头和正文的HttpClientRequestVisualC#》经验,为你挑选了1个好方法。

我想向HttpClient发送以下格式的请求:标头:

X-Requested-With: XMLHttpRequest
User-Agent: .....  //etc.

重要的部分是使我感到困惑的请求正文:

{"jsonrpc":"2.0","method":"getPostList","id":1,"params":[[["place2",0,["10"]]],0]}

我做了一些这样的代码:

HttpClient h = new HttpClient();

h.DefaultRequestHeaders.Add("X-Requested-With","XMLHttpRequest");
h.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0");
h.DefaultRequestHeaders.Add("Referer","http://urmia.divar.ir/browse/");
h.DefaultRequestHeaders.Add("Pragma","no-cache");
h.DefaultRequestHeaders.Add("Host","urmia.divar.ir");
//h.DefaultRequestHeaders.Add("Content-Type","application/json; charset=UTF-8");
h.DefaultRequestHeaders.Add("Connection","keep-alive");
h.DefaultRequestHeaders.Add("Accept-Language","en-US,en;q=0.5");
h.DefaultRequestHeaders.Add("Accept-Encoding","gzip, deflate");
h.DefaultRequestHeaders.Add("Accept","application/json, text/Javascript, */*; q=0.01");

var request = new HttpRequestMessage(HttpMethod.Post, "http://urmia.divar.ir/json/");

request.Content.Headers.AddWithoutValidation("jsonrpc","2.0");
request.Content.Headers.Add("method", "getPostList");
request.Content.Headers.Add("id", "1");
request.Content.Headers.Add("params", "[[[\"place2\",0,[\"10\"]]],0]");

但是它为request.Content.Headers.add ....提供了错误。



1> BitTickler..:

不知道该请求返回什么-但似乎是二进制的...这就是我设法获得响应的方式-可能是这样的:)因此,如示例代码所示,我将JSON请求对象放入了请求消息。

using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.IO;

namespace CSharpQuestions
{
    class Program
    {
        static Stream GenerateStreamFromString(string s)
        {
            MemoryStream stream = new MemoryStream();
            StreamWriter writer = new StreamWriter(stream);
            writer.Write(s);
            writer.Flush();
            stream.Position = 0;
            return stream;
        }

        static void Main(string[] args)
        {
            HttpClient h = new HttpClient();

            h.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");
            h.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0");
            h.DefaultRequestHeaders.Add("Referer", "http://urmia.divar.ir/browse/");
            h.DefaultRequestHeaders.Add("Pragma", "no-cache");
            h.DefaultRequestHeaders.Add("Host", "urmia.divar.ir");
            // h.DefaultRequestHeaders.Add("Content-Type","application/json; charset=UTF-8");
            h.DefaultRequestHeaders.Add("Connection", "keep-alive");
            h.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.5");
            h.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate");
            h.DefaultRequestHeaders.Add("Accept", "application/json, text/Javascript, */*; q=0.01");

            string myJSOnRequest= "{\"jsonrpc\":\"2.0\",\"method\":\"getPostList\",\"id\":1,\"params\":[[[\"place2\",0,[\"10\"]]],0]}";
            HttpContent requestCOntent= new StreamContent(GenerateStreamFromString(myJSONRequest));
            Task respOnse= h.PostAsync("http://urmia.divar.ir/json/", requestContent);
            response.Wait(3000);
            byte[] respOnseText= response.Result.Content.ReadAsByteArrayAsync().Result;
            System.Console.WriteLine(responseText); // you would know what to do with the data ;)

        }

    }
}


推荐阅读
author-avatar
mobiledu2502921803
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有