Получение ошибки 400 http bad request при доступе к службе отдыха WCF с помощью GETC#

Место общения программистов C#
Ответить
Anonymous
 Получение ошибки 400 http bad request при доступе к службе отдыха WCF с помощью GET

Сообщение Anonymous »

Точная ошибка: System.Net.WebException: удаленный сервер возвратил ошибку: (400) Неверный запрос. в system.Net.HttpWebRequest.GetResponse() у меня есть служба отдыха WCF, размещенная в IIS. Я создал простое приложение WPF с помощью всего лишь кнопки. Теперь мне нужно получить доступ к методам в моей службе WCF, используя get..

Это код для WPF

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Runtime.Serialization;
using System.Xml;
namespace RestFulDemo
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void RestDemo_Click(object sender, RoutedEventArgs e)
{
string msg="";
HttpWebResponse resp=null;
//WebRequest my = WebRequest.Create(@"http://www.google.com");
//MessageBox.Show(my.ToString());
WebRequest myRequest = WebRequest.Create(@"http://localhost/REST/RestServicesSampl ... mlData/sad");
MessageBox.Show(myRequest.ToString());
//Provides response from a URI.
myRequest.Method = "GET";
myRequest.ContentType = @"text/xml; charset=utf-8";
try
{
resp=myRequest.GetResponse() as HttpWebResponse;
}
catch (WebException c)
{
MessageBox.Show(c.ToString());
}

if (resp.StatusCode == HttpStatusCode.OK)
{
XmlDocument myXMLDocument = new XmlDocument();
XmlReader myXMLReader = new XmlTextReader(resp.GetResponseStream());
myXMLDocument.Load(myXMLReader);
msg= myXMLDocument.InnerText;
}
MessageBox.Show(msg);
}
}
}


почему я получаю сообщение об ошибке в myRequest.GetResponse()..?
это мой файл .svc.cs

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

namespace RestServices
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IRestServicesSample" in both code and config file together.
[ServiceContract]
public interface IRestServicesSample
{
[OperationContract]
[WebGet]
////[WebInvoke(Method = "GET",
//// ResponseFormat = WebMessageFormat.Xml,
//// BodyStyle = WebMessageBodyStyle.Wrapped,
//// UriTemplate = "xml/{id}")]
string XmlData(string id);
//void DoWork();
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/{id}")]
string JsonData(string id);
}
}


--- это файл web.config


















`enter code here`


























Подробнее здесь: https://stackoverflow.com/questions/199 ... -using-get
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»