在另一个网站上输入数据并使用c#获取回复

时间:2016-01-29 09:52:21

标签: c# asp.net-mvc fiddler

我想将数据发送到http://irsherkat.ssaa.ir/Design/SearchCompanyPublicInfo.aspx,其值为#8xt1677542,用于#txtNationalCode输入。当我使用Fiddler向http://irsherkat.ssaa.ir/hndlr/GeneralHandler.ashx?Id=33发送请求时(已发布数据已发送到),请使用此请求标头:

Content-Type: application/x-www-form-urlencoded

和此请求正文:

Request={'ClassTypeName':'TRM.hndlr.Classes.CompanyPublicInfoController','MethodName':'SearchData','DataParamType':'TRM.hndlr.TRMTypeDefinition.CompanyPublicInfo.CompanyPublicInfoSerchParam','MainEntityType':'','DataParam':'{\'Nationalcode\':\'10861677542\',\'Registernumber\':\'\',\'UnitId\':null,\'CompanyTypeId\':\'0\'}'}

我获得了200个成功结果,但是当我想用这个C#代码执行此操作时:

public async Task<string> Get()
    {
        var request = (HttpWebRequest)WebRequest.Create("http://irsherkat.ssaa.ir/hndlr/GeneralHandler.ashx?Id=33");

        var postData = "Request={'ClassTypeName':'TRM.hndlr.Classes.CompanyPublicInfoController','MethodName':'SearchData','DataParamType':'TRM.hndlr.TRMTypeDefinition.CompanyPublicInfo.CompanyPublicInfoSerchParam','MainEntityType':'','DataParam':'{\'Nationalcode\':\'10861677542\',\'Registernumber\':\'\',\'UnitId\':null,\'CompanyTypeId\':\'0\'}'}";
        var data = Encoding.ASCII.GetBytes(postData);

        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = data.Length;

        using (var stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }

        var response = (HttpWebResponse)request.GetResponse();

        return new StreamReader(response.GetResponseStream()).ReadToEnd();
    }

它给了我这个错误:

The remote server returned an error: (500) Internal Server Error.

1 个答案:

答案 0 :(得分:0)

尝试从"Request="值中删除postData