【问题标题】:WCF service:400 bad request?WCF 服务:400 错误请求?
【发布时间】:2015-11-24 01:05:18
【问题描述】:

我在 wcf 上工作了 7 天。我创建了一个 Wcf 服务。 但是工作正常并通过添加服务参考在 Visual Studio 中得到响应。但我的问题是,每当它集成到 android/windows 中时,那些人都没有得到响应。他们得到 400(错误请求)。请任何人帮助我如何得到正确的回应。 (我想要响应为 json)这是我下面的代码。 提前谢谢你。

在界面中

public interface ISampleService1
    {
        [OperationContract]

         [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "getdata",BodyStyle = WebMessageBodyStyle.Bare)]
         string getdata();
}

在课堂上

 public  string getdata()
        {
            try
            {


                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con2"].ConnectionString);
                SqlCommand comm = new SqlCommand("select CircleID, CircleName from Circle", con);
                con.Open();
                comm.ExecuteNonQuery();

                SqlDataAdapter da = new SqlDataAdapter(comm);
                // DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                da.Fill(dt);

                JavaScriptSerializer JSSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                List<Dictionary<string, object>> DtRows = new List<Dictionary<string, object>>();
                Dictionary<string, object> newrow = null;

                //Code to loop each row in the datatable and add it to the dictionary object
                foreach (DataRow drow in dt.Rows)
                {
                    newrow = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        newrow.Add(col.ColumnName.Trim(), drow[col]);
                    }
                    DtRows.Add(newrow);
                }
                con.Close();


                return JSSerializer.Serialize(DtRows);
            }
            catch (Exception ex)
            {

                List<String> Parameters = new List<String>();
                Parameters.Add("getRechargeCircleList");

             //   SendErrorMail(ex.ToString(), "getRechargeCircleList", Parameters);
               // Context.Response.ContentType = "application/json; charset=utf-8";
               // Context.Response.Write(@" {""Status"":""false"",""Message"" : ""Oops! Something went Wrong""}");
               // return;
            }
            return "false";
        }

web.config 文件:

<?xml version="1.0"?>
<configuration>
  <appSettings/>




  </connectionStrings>


  <system.web>

    <httpRuntime maxRequestLength="32768"/>
    <compilation debug="true" targetFramework="4.0"/>
     <customErrors mode="Off"/>
    <!--
        The <authentication> section enables configuration 
        of the security authentication mode used by 
        ASP.NET to identify an incoming user. 
    -->
   <authentication mode="Windows"/>
    <!--
        The <customErrors> section enables configuration 
        of what to do if/when an unhandled error occurs 
        during the execution of a request. Specifically, 
        it enables developers to configure html error pages 
        to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
         <error statusCode="403" redirect="NoAccess.htm" />
         <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
    -->
    <pages controlRenderingCompatibilityVersion="4.0" clientIDMode="AutoID"/>
  </system.web>
  <!-- 
      The system.webServer section is required for running ASP.NET AJAX under Internet
      Information Services 7.0.  It is not necessary for previous version of IIS.
  -->
  <system.webServer>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
  <system.serviceModel>

    <bindings>
    <basicHttpBinding>
        <binding name="basicHttp" allowCookies="true"
                 maxReceivedMessageSize="4194304" 
                 maxBufferSize="20000000"
                 maxBufferPoolSize="20000000">
            <readerQuotas maxDepth="32" 
                 maxArrayLength="200000000"
                 maxStringContentLength="200000000"/>
        </binding>
    </basicHttpBinding>
</bindings>
    <services>
      <service behaviorConfiguration="SampleWcfproject.Service1Behavior" name="SampleWcfproject.Service1">
        <endpoint address="" binding="basicHttpBinding" contract="SampleWcfproject.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
      <service behaviorConfiguration="SampleWcfproject.SampleService1Behavior" name="SampleWcfproject.SampleService1">
        <endpoint address="" binding="basicHttpBinding" contract="SampleWcfproject.ISampleService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
       <endpointBehaviors>
            <behavior name="web">
                <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
            </behavior>
        </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="SampleWcfproject.Service1Behavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
        <behavior name="SampleWcfproject.SampleService1Behavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

  </system.serviceModel>
</configuration>

我通过使用 Web 参考项目在 Visual Studio 中得到以下响应。但在 android 中出现类似 400 的错误。

[{""CircleID"":1,""CircleName"":""安得拉邦""},{""CircleID"":4,""CircleName"":""阿萨姆邦""},{ ""CircleID"":5,""CircleName"":""Bihar""},{""CircleID"":6,""CircleName"":""Chennai""},{""CircleID"": 7,""CircleName"":""Delhi""},{""CircleID"":8,""CircleName"":""Gujarat""},{""CircleID"":9,""CircleName" ":""喜马偕尔邦""},{""CircleID"":10,""CircleName"":""哈里亚纳邦""},{""CircleID"":11,""CircleName"":""查谟\u0026 克什米尔""},{""CircleID"":12,""CircleName"":""Kerala""},{""CircleID"":13,""CircleName"":""Karnataka""} ,{""CircleID"":14,""CircleName"":""加尔各答""},{""CircleID"":15,""CircleName"":""马哈拉施特拉邦""},{""CircleID" ":16,""CircleName"":""Madhya Pradesh""},{""CircleID"":17,""CircleName"":""Mumbai""},{""CircleID"":18," "CircleName"":""North East""},{""CircleID"":19,""CircleName"":""Orissa""},{""CircleID"":20,""CircleName"": ""Punjab""},{""CircleID"":21,""CircleName"":""Rajasthan""},{""CircleID"":22,""CircleName"}]");

这是我的网址 http://xyzmnmkljj.in/SampleService1.svc/getdata

但它给出了 400 个错误请求。

对 web.comfig 文件的任何修改请帮助我

【问题讨论】:

    标签: c# android asp.net wcf http-status-code-400


    【解决方案1】:

    您能否检查您的 IP 是否可供这些机器访问,如果是,那么您需要授予该用户访问该服务的权限。在这种情况下,您需要在提供程序中将 IIS 设置更改为 NTLM。

    blogs.msdn.com

    【讨论】:

      猜你喜欢
      • 2012-04-27
      • 1970-01-01
      • 2011-09-29
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 2013-06-22
      • 2017-08-16
      • 1970-01-01
      相关资源
      最近更新 更多