WCF服务:400个错误请求?

时间:2015-08-29 13:55:07

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

我从7天开始研究wcf。我创建了一个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中的以下respomse。但是在android中获得400的错误。

[{“”CircleID“”:1,“”CircleName“”:“”Andhra Pradesh“”},{“”CircleID“”:4,“”CircleName“”:“”Assam“”},{ “” CircleID “”:5 “” CIRCLENAME “”: “” 比哈尔邦 “”},{ “” CircleID “”:6 “” CIRCLENAME “”: “” 奈 “”},{ “” CircleID “”: 7 “” CIRCLENAME “”: “” 德里 “”},{ “” CircleID “”:8 “” CIRCLENAME “”: “” 古吉拉特 “”},{ “” CircleID “”:9 “” CIRCLENAME” “:”“Himachal Pradesh”“},{”“CircleID”“:10,”“CircleName”“:”“Haryana”“},{”“CircleID”“:11,”“CircleName”“:”“查谟\“Kashmir”“},{”“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”“: “” 旁遮普 “”},{ “” CircleID “”:21 “” CIRCLENAME “”: “” 拉贾斯坦邦 “”},{ “” CircleID “”:22 “” CIRCLENAME “}]”);

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

但它提出了400个不良请求。

web.comfig文件中的任何修改都可以帮助我

1 个答案:

答案 0 :(得分:0)

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

blogs.msdn.com