简体   繁体   English

来自本地WCF服务的HttpWebRequest

[英]HttpWebRequest from a local WCF service

I am doing some testing for a Xamarin Android app with a simple local WCF service to prove my connection code works. 我正在使用简单的本地WCF服务对Xamarin Android应用进行一些测试,以证明我的连接代码有效。

Service: [OperationContract] string Ping(); 服务:[OperationContract]字符串Ping(); … public string Ping() { return "Pong"; …公共字符串Ping(){返回“ Pong”; } }

Test Code in Xamarin App: Xamarin App中的测试代码:

var request = HttpWebRequest.Create(string.Format(@"http://192.168.1.175/_Services/TestService1.svc/Ping"));

request.Credentials = CredentialCache.DefaultCredentials;
request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
request.ContentLength = 0; //pass.Length;
request.Method = "POST";

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)  //Errors out here
{
  using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  {
    var content = reader.ReadToEnd();
    Console.Out.WriteLine("Response Body: \r\n {0}", content);
  }
}

Error: 错误:

The remote server returned an error: (400) Bad Request. 远程服务器返回错误:(400)错误的请求。

Edit: 编辑:

When using ServiceReference, the following works: 使用ServiceReference时,以下工作:

private void button3_Click(object sender, EventArgs e) { ServiceReference1.TestService1Client client = new ServiceReference1.TestService1Client(); private void button3_Click(object sender,EventArgs e){ServiceReference1.TestService1Client client = new ServiceReference1.TestService1Client();

        string returnString;

        returnString = client.Ping();
        label1.Text = returnString;
    }

Slightly different code still does not work: private void button4_Click(object sender, EventArgs e) { //string serviceUrl = " http://192.168.1.175/_Services/TestService1.svc "; 稍有不同的代码仍然不起作用:private void button4_Click(object sender,EventArgs e){// string serviceUrl =“ http://192.168.1.175/_Services/TestService1.svc ”; string serviceUrl = " http://localhost/_Services/TestService1.svc "; 字符串serviceUrl =“ http://localhost/_Services/TestService1.svc ”;

        HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(new Uri(serviceUrl + "/Ping"));
        httpRequest.Accept = "text/xml";
        httpRequest.ContentType = "text/xml";
        httpRequest.Method = "POST";
        httpRequest.ContentLength = 0;
        httpRequest.KeepAlive = false;

        using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse()) //400 Bad Request
        {
            using (Stream stream = httpResponse.GetResponseStream())
            {
                label1.Text = (new StreamReader(stream)).ReadToEnd();
            }
        }
    }

The answer was rooted in System.ServiceModel.Activation.WebServiceHostFactory 答案植根于System.ServiceModel.Activation.WebServiceHostFactory

For some reason none of my sources mentioned this during research for using HttpWebRequest. 由于某种原因,在使用HttpWebRequest的研究过程中,我的任何资料都没有提到这一点。

I found the reference by chance when looking at Android WCF consuming. 在查看Android WCF的使用时,偶然发现了该参考。 https://minafayek.wordpress.com/2013/04/02/consuming-iis-published-restful-wcf-service-from-android-over-wifi/ https://minafayek.wordpress.com/2013/04/02/using-iis-published-restful-wcf-service-from-android-over-wifi/

I got my testing programs working so, I should be able to move forward. 我的测试程序正常运行,因此我应该能够继续前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM