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

2024-09-30 20:32:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个示例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版本那样返回任何数据,只是错误地认为请求不正确:

^{pr2}$

json看起来有什么问题吗?在

谢谢


Tags: importreportjsonurldatatableurllib2urllib
2条回答

试试这个。。在

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
{

}   

通过将内容类型更改为:

_webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

我在与charles进行调试,python版本发布了一个json格式的表单,将c#内容类型更改为匹配,现在开始工作了

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

相关问题 更多 >