【发布时间】:2018-12-31 03:06:42
【问题描述】:
我正在使用简单的本地 WCF 服务对 Xamarin Android 应用程序进行一些测试,以证明我的连接代码有效。
服务: [运营合同] 字符串平(); … 公共字符串 Ping() { 返回“乒乓”; }
Xamarin 应用中的测试代码:
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);
}
}
错误:
远程服务器返回错误:(400) Bad Request。
编辑:
使用 ServiceReference 时,以下工作:
private void button3_Click(object sender, EventArgs e) { ServiceReference1.TestService1Client 客户端 = new ServiceReference1.TestService1Client();
string returnString;
returnString = client.Ping();
label1.Text = returnString;
}
稍有不同的代码仍然不起作用: 私人无效按钮4_Click(对象发送者,EventArgs e) { //字符串 serviceUrl = "http://192.168.1.175/_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();
}
}
}
【问题讨论】:
-
不清楚你在这里问什么?你都尝试了些什么?其他客户端可以连接吗?如果可能,请提供Short, Self Contained, Correct (Compilable), Example。
-
@Rangi Keen 试图找出“远程服务器返回错误:(400) 错误请求。”在标记的代码行上。
-
我的 WCF 服务在使用服务参考的桌面表单应用程序中工作。到目前为止,Xamarin 应用程序没有运气。
标签: c# httpwebrequest