c#POST json没有按预期工作?

时间:2015-07-21 11:06:22

标签: c# python json

我有这个示例python脚本,我试图使用newtonsoft转换为c#:

import urllib
import urllib2
import json
url = 'http://server.net/fcgi/scrut_fcgi.fcgi'
report_details = {
'reportTypeLang' : 'conversations',
'reportDirections' : {
'selected' : 'inbound'
},
'times' : {
'dateRange' : 'Last5Minutes'
},
'filters' : {
'sdfDips_0' : 'in_AC10033A_AC10033A-410'
},
'dataGranularity' : {
'selected' : 'auto'
}
}
data_i_need = {
'inbound' : {
'table' : {
'query_limit' : {
'offset' : 0,
'max_num_rows' : 1
}
}
},
'outbound' : {
'table' : {
'query_limit' : {
'offset' : 0,
'max_num_rows' : 1
}
}
}
}
data = {
'rm' : 'report_api',
'action' : 'get',
'rpt_json' : json.dumps( report_details ),
'data_requested' : json.dumps( data_i_need )
}

data = urllib.urlencode( data )
req = urllib2.Request( url, data )
response = urllib2.urlopen( req )   
report = response.read()
report_obj = json.loads( report )

到目前为止,在一些帮助下,我有以下c#代码,但它不会返回任何数据,如python版本只是错误,就像请求不正确一样:

class Program
{
    static void Main(string[] args)
    {
        ReportDirections reportDirections = new ReportDirections();
        reportDirections.selected = "inbound";

        Times Times = new Times();
        Times.dateRange = "Last5Minutes";

        Filters Filters = new Filters();
        Filters.sdfDips_0 = "in_AC10033A_AC10033A-410";

        DataGranularity DataGranularity = new DataGranularity();
        DataGranularity.selected = "auto";

        ReportDetails ReportDetails = new ReportDetails();
        ReportDetails.reportTypeLang = "conversations";
        ReportDetails.reportDirections = reportDirections;
        ReportDetails.times = Times;
        ReportDetails.filters = Filters;
        ReportDetails.dataGranularity = DataGranularity;
        //
        QueryLimit QueryLimit = new QueryLimit();
        QueryLimit.offset = 0;
        QueryLimit.max_num_rows = 1;

        QueryLimit2 QueryLimit2 = new QueryLimit2();
        QueryLimit2.offset = 0;
        QueryLimit2.max_num_rows = 1;

        Table Table = new Table();
        Table.query_limit = QueryLimit;

        Table2 Table2 = new Table2();
        Table2.query_limit = QueryLimit2;

        Inbound Inbound = new Inbound();
        Inbound.table = Table;

        Outbound Outbound = new Outbound();
        Outbound.table = Table2;

        DataINeed DataINeed = new DataINeed();
        DataINeed.inbound = Inbound;
        DataINeed.outbound = Outbound;

        var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://server.bobo.net/fcgi/scrut_fcgi.fcgi");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";

        var serializer = new Newtonsoft.Json.JsonSerializer();
        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            using (var tw = new Newtonsoft.Json.JsonTextWriter(streamWriter))
            {
                serializer.Serialize(tw,
                    new
                    {
                        action = "get",
                        rm = "report_api",
                        data_requested = DataINeed,
                        rpt_json = ReportDetails
                    });
            }
        }
        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var responseText = streamReader.ReadToEnd();
            Console.WriteLine(responseText);
        }
    }
}
public class tw
{
    public string rm { get; set; }
    public string action { get; set; }
    public string rpt_json { get; set; }
    public string data_requested { get; set; }
}
public class DataINeed
{
    public Inbound inbound { get; set; }
    public Outbound outbound { get; set; }
}
public class Inbound
{
    public Table table { get; set; }
}
public class Outbound
{
    public Table2 table { get; set; }
}
public class Table
{
    public QueryLimit query_limit { get; set; }
}
public class Table2
{
    public QueryLimit2 query_limit { get; set; }
}
public class QueryLimit
{
    public int offset { get; set; }
    public int max_num_rows { get; set; }
}
public class QueryLimit2
{
    public int offset { get; set; }
    public int max_num_rows { get; set; }
}
public class ReportDetails
{
    public string reportTypeLang { get; set; }
    public ReportDirections reportDirections { get; set; }
    public Times times { get; set; }
    public Filters filters { get; set; }
    public DataGranularity dataGranularity { get; set; }
}
public class ReportDirections
{
    public string selected { get; set; }
}
public class Times
{
    public string dateRange { get; set; }
}
public class Filters
{
    public string sdfDips_0 { get; set; }
}

public class DataGranularity
{
    public string selected { get; set; }
}
public class bob
{

}   

这看起来像是发布json请求的正确方法还是做错了什么?

由于

2 个答案:

答案 0 :(得分:1)

试试这个..

using System.Net;
using Newtonsoft.Json;  
class Program
{
    static void Main(string[] args)
    {
        ReportDirections reportDirections = new ReportDirections();
        reportDirections.selected = "inbound";

        Times Times = new Times();
        Times.dateRange = "Last5Minutes";

        Filters Filters = new Filters();
        Filters.sdfDips_0 = "in_AC10033A_AC10033A-410";

        DataGranularity DataGranularity = new DataGranularity();
        DataGranularity.selected = "auto";

        ReportDetails ReportDetails = new ReportDetails();
        ReportDetails.reportTypeLang = "conversations";
        ReportDetails.reportDirections = reportDirections;
        ReportDetails.times = Times;
        ReportDetails.filters = Filters;
        ReportDetails.dataGranularity = DataGranularity;
        //
        QueryLimit QueryLimit = new QueryLimit();
        QueryLimit.offset = 0;
        QueryLimit.max_num_rows = 1;

        QueryLimit2 QueryLimit2 = new QueryLimit2();
        QueryLimit2.offset = 0;
        QueryLimit2.max_num_rows = 1;

        Table Table = new Table();
        Table.query_limit = QueryLimit;

        Table2 Table2 = new Table2();
        Table2.query_limit = QueryLimit2;

        Inbound Inbound = new Inbound();
        Inbound.table = Table;

        Outbound Outbound = new Outbound();
        Outbound.table = Table2;

        DataINeed DataINeed = new DataINeed();
        DataINeed.inbound = Inbound;
        DataINeed.outbound = Outbound;

        WebClient _webClient = new WebClient();
        _webClient.Headers.Add("Content-Type", "application/json");
        string data_requested = HttpUtility.UrlEncode(JsonConvert.SerializeObject(DataINeed));
        string rpt_json = HttpUtility.UrlEncode(JsonConvert.SerializeObject(ReportDetails));

        string data = "action=get&rm=report_api&data_requested=" + data_requested + "&rpt_json="+rpt_json;

        string address = "http://server/fcgi/scrut_fcgi.fcgi";
        var responseText = Encoding.Default.GetString(_webClient.UploadData(address, "POST", Encoding.Default.GetBytes(data)));
        Console.WriteLine(responseText);
    }
}
public class tw
{
    public string rm { get; set; }
    public string action { get; set; }
    public string rpt_json { get; set; }
    public string data_requested { get; set; }
}
public class DataINeed
{
    public Inbound inbound { get; set; }
    public Outbound outbound { get; set; }
}
public class Inbound
{
    public Table table { get; set; }
}
public class Outbound
{
    public Table2 table { get; set; }
}
public class Table
{
    public QueryLimit query_limit { get; set; }
}
public class Table2
{
    public QueryLimit2 query_limit { get; set; }
}
public class QueryLimit
{
    public int offset { get; set; }
    public int max_num_rows { get; set; }
}
public class QueryLimit2
{
    public int offset { get; set; }
    public int max_num_rows { get; set; }
}
public class ReportDetails
{
    public string reportTypeLang { get; set; }
    public ReportDirections reportDirections { get; set; }
    public Times times { get; set; }
    public Filters filters { get; set; }
    public DataGranularity dataGranularity { get; set; }
}
public class ReportDirections
{
    public string selected { get; set; }
}
public class Times
{
    public string dateRange { get; set; }
}
public class Filters
{
    public string sdfDips_0 { get; set; }
}

public class DataGranularity
{
    public string selected { get; set; }
}
public class bob
{

}   

答案 1 :(得分:0)

设法通过将内容类型更改为:

来使其工作
_webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

我正在调查查尔斯并且python版本发布了一个json格式化表单,更改了c#内容类型以匹配,现在可以处理

非常感谢您的代码和帮助。