使用xamarin.forms的Wcf:System.Net.WebException状态码407(需要ProxyAuthentication):需要身份验证

时间:2018-07-24 17:28:36

标签: .net visual-studio wcf xamarin.forms

我在工作中使用xamarin.forms,他们需要我使用wcf将移动应用程序连接到他们的sql服务器。我的wcf服务正在运行,我可以获取数据并将其发送到数据库,但是在尝试使用android模拟器时出现错误。

Web.config

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

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1"/>
  </system.web>

  <system.serviceModel>


        <!--<behavior>
          --><!-- To avoid disclosing metadata information, set the values below to false before deployment --><!--
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          --><!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information --><!--
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>-->
    <bindings>
      <webHttpBinding>
        <binding name="restLargeBinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="Streamed">
          <readerQuotas maxStringContentLength="2147483647"/>
        </binding>


      </webHttpBinding>

    </bindings>


    <behaviors>
      <endpointBehaviors>
        <behavior name="MyWebEndpointBehaviour">
          <webHttp automaticFormatSelectionEnabled="true" defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" helpEnabled="true"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>

        </behavior>

      </endpointBehaviors>

      <serviceBehaviors>
        <behavior name="MyServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>

      </serviceBehaviors>
    </behaviors>

    <services>
      <service name="WcfService1.Service1" behaviorConfiguration="MyServiceBehaviour">
        <endpoint address="" contract="WcfService1.IService1" binding="webHttpBinding" bindingConfiguration="restLargeBinding" behaviorConfiguration="MyWebEndpointBehaviour"/>
        <endpoint address="mex" contract="WcfService1.IService1" binding="mexHttpBinding"/>

      </service>

    </services>


    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        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.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
      <bypasslist>
        <clear />
      </bypasslist>
      <proxy proxyaddress="proxy.njc.ups.com:8080"
             usesystemdefault="false"
             bypassonlocal="true"
             autoDetect="False" />
    </defaultProxy>
  </system.net>
</configuration>

Service1.svc

        using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;

    namespace WcfService1
    {




        public class Service1 : IService1
            {

                //Connection String//
                private string conStr = "Data Source=WKSP0009A982; Initial Catalog=MyDatabase; Integrated Security=true";

                public List<UserNames> GetUserName()
                {
                    List<UserNames> nameList = new List<UserNames>();
                    SqlConnection connection = new SqlConnection(this.conStr);
                    connection.Open();
                    SqlCommand cmd = new SqlCommand("SELECT Names FROM UserNames", connection);
                    cmd.CommandType = CommandType.Text;
                    SqlDataReader sdr = cmd.ExecuteReader();

                    while (sdr.Read())
                    {
                        UserNames names = new UserNames();
                        names.Names = sdr["Names"].ToString();
                        nameList.Add(names);
                    }
                    return nameList.ToList();
                }

                public int Addname(string name)
                {
                    int status = 0;
                    SqlConnection connection = new SqlConnection(this.conStr);
                    SqlCommand cmd = new SqlCommand();
                    try
                    {

                        if (connection.State == ConnectionState.Closed)
                        {
                            connection.Open();
                        }


                        cmd = new SqlCommand("INSERT INTO UserNames(Names) VALUES (@name)", connection);
                        cmd.CommandType = CommandType.Text;
                        cmd.Parameters.AddWithValue("@name", name);
                        cmd.ExecuteReader();
                        status = 1;
                    }

                    catch(Exception ex)
                    {

                        throw ex;
                    }

                    finally
                    {
                        connection.Close();
                        cmd.Dispose();
                    }
                    return status;


                 }


            }


    }

IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        //GET METHOD
        [OperationContract]
        [WebGet(UriTemplate = "GetUserName")]
        List<UserNames> GetUserName();

        //POST METHOD
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "AddName", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        int Addname(string name);
    }

    [DataContract]
    public class UserNames
    {
        string names;

        [DataMember]
        public string Names
        {
            get { return names; }
            set { names = value; }
        }
    }



}

还有我的MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ServiceReference1;
using Xamarin.Forms;

namespace MyApp
{
    public partial class MainPage : ContentPage

    {
        ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();
        public MainPage()
        {
            InitializeComponent();
        }

        async void  SubmitButton_Clicked(object sender, EventArgs e)
        {
            UserNames userInfo = new UserNames();
            userInfo.Names = UserEntry.Text;

            await service.AddnameAsync(userInfo.Names);



        }
    }
}

现在我的实际应用程序具有更多功能,但是出于测试目的,我制作了这个简单的应用程序,只是为了从数据库中调用信息并将信息发送到便携式计算机甚至我的手机上的android仿真器。现在公司代理可能是一个问题,但是即使我使用自己的互联网热点,我仍然会收到诸如代理名称解析之类的错误。...请帮助

0 个答案:

没有答案