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

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

Сообщение Anonymous »

Точная ошибка - system.net.webexception: удаленный сервер вернул ошибку: (400) Плохой запрос. В System.net.httpwebrequest.getResponse () у меня есть служба REST WCF, размещенную в IIS.. Я создал простое приложение 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);
}
}
}
< /code>

Почему я получу ошибку в myRequest.getResponse () ..?
Это мой файл .svc.cs < /p>

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);
}
}
< /code>

--- Это файл web.config < /p>


















`enter code here`


























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

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

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

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

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

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

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