Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果内容长度>;则无法在WebRequest上发布;7kb使用C#_C#_Json_Web Services_Httpwebrequest_Httpwebresponse - Fatal编程技术网

如果内容长度>;则无法在WebRequest上发布;7kb使用C#

如果内容长度>;则无法在WebRequest上发布;7kb使用C#,c#,json,web-services,httpwebrequest,httpwebresponse,C#,Json,Web Services,Httpwebrequest,Httpwebresponse,我正在尝试向我们的web服务发布Json。我的问题是,当我的request.ContentLength超过7kb时。我将在request.GetResponse()时获得一个web异常500。但如果我的request.ContentLength低于7kb,我的帖子将成功发送 错误消息: The remote server returned an error: (500) Internal Server Error. 源代码: public static string JsonPost(str

我正在尝试向我们的web服务发布Json。我的问题是,当我的request.ContentLength超过7kb时。我将在request.GetResponse()时获得一个web异常500。但如果我的request.ContentLength低于7kb,我的帖子将成功发送

错误消息:

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

public static string JsonPost(string url, string method, string postData)
{
    Uri address = new Uri(url + method);
    HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
    request.Method = "POST";
    request.ContentType = "application/json";
    byte[] byteData = UTF8Encoding.UTF8.GetBytes(postData);
    request.ContentLength = byteData.Length;
    using (Stream postStream = request.GetRequestStream())
    {
        postStream.Write(byteData, 0, byteData.Length);
    }
    try
    {
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string JsonResponse = reader.ReadToEnd();
            return JsonResponse;
        }
    }
    catch (WebException ex)
    {
        string message = ((System.Net.HttpWebResponse)(ex.Response)).StatusDescription;
        if (message.Contains("busy"))
            return message;
        else
            throw new Exception(message);
    }
}
Web.config:

        <?xml version="1.0"?>

<configuration>
  <appSettings>
    <add key="webpages:Version" value="1.0.0.0"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
  </appSettings>

  <system.web>
    <httpRuntime executionTimeout="180" maxRequestLength="1048576" useFullyQualifiedRedirectUrl="false" minFreeThreads="8"
    minLocalRequestFreeThreads="4" appRequestQueueLimit="100" enableVersionHeader="true" />

    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>

    <authentication mode="Forms">
      <forms loginUrl="~/Home/" cookieless="AutoDetect" timeout="2880" />
    </authentication>

    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>

    <profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
      </providers>
    </profile>

    <roleManager enabled="false">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>

    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages"/>
      </namespaces>
    </pages>
  </system.web>

  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824" />
      </requestFiltering>
    </security>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

您可能必须在web.config中启用大文件支持(httpRuntime,maxRequestLength参数)。
这是一个示例

<httpRuntime 
 ...
 ...
 maxRequestLength="20485760" 
 ...
 ...
 .../>

试试这个:

%windir%\system32\inetsrv\appcmd set config -section:requestFiltering -requestLimits.maxAllowedContentLength:1000000
或者,如果您只想为您的应用程序设置:

%windir%\system32\inetsrv\appcmd set config "Default Web Site/<your app>" -section:requestFiltering -requestLimits.maxAllowedContentLength:1000000
%windir%\system32\inetsrv\appcmd set config“Default Web Site/”-节:requestFiltering-requestLimits.maxAllowedContentLength:1000000
你可以找到更多的限制选项


希望这有帮助

听起来问题可能出在web服务中……嗨,Jon,嗯,web服务被配置为允许2147483647最大请求长度。我们还试图设置一个断点,但web服务没有收到任何帖子。如果内容长度>7kb,则得到500-这表明某个地方存在内部错误。我建议您仔细查看所有可用的日志。您正在使用哪个浏览器进行测试?此外,你的7kb+测试帖子是否包含较小帖子没有的字符?在chrome、firefox和safari上测试。7kb+测试包含base64字符串,它是一个映像。我已经在我的web.config上设置了httpRuntime maxRequestLength=“1048576”,并且在system.WebServer上设置了以下代码。请发布您的web.config的完整httpRuntime。谢谢,这真的很有帮助。对不起,如果我忘了将其标记为已回答。谢谢
%windir%\system32\inetsrv\appcmd set config "Default Web Site/<your app>" -section:requestFiltering -requestLimits.maxAllowedContentLength:1000000