Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting 400 http bad request error while accessing WCF rest service using GET
    primarykey
    data
    text
    <p><a href="http://i.stack.imgur.com/kd7Y4.png" rel="nofollow">the Exact error is System.Net.WebException: The Remote server returned an Error: (400) Bad Request. at system.Net.HttpWebRequest.GetResponse()</a>i have a WCF rest service hosted in IIS..I have created a simple WPF app with just a button..now i need to access the methods in my WCF service using get..</p> <p>This is the code For WPF</p> <pre><code>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 { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; 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/RestServicesSample.svc/XmlData/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></pre> <p>why do i get an error in myRequest.GetResponse()..? this is my .svc.cs file</p> <pre><code>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></pre> <p>---this is the web.config file</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;configuration&gt; &lt;system.web&gt; &lt;compilation debug="true" targetFramework="4.0" /&gt; &lt;webServices&gt; &lt;protocols&gt; &lt;add name="HttpGet"/&gt; &lt;add name="HttpPost"/&gt; &lt;/protocols&gt; &lt;/webServices&gt; &lt;/system.web&gt; &lt;system.serviceModel&gt; &lt;services&gt; &lt;service name="RestServices.RestServiceSample" behaviorConfiguration="ServiceBehaviour"&gt; &lt;endpoint behaviorConfiguration="WebBehavior" binding="webHttpBinding" contract="RestServices.IRestServiceSample"&gt; &lt;/endpoint&gt; &lt;/service&gt;`enter code here` &lt;/services&gt; &lt;behaviors&gt; &lt;serviceBehaviors&gt; &lt;behavior name="ServiceBehaviour"&gt; &lt;!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --&gt; &lt;serviceMetadata httpGetEnabled="true"/&gt; &lt;!-- 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 --&gt; &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt; &lt;/behavior&gt; &lt;/serviceBehaviors&gt; &lt;endpointBehaviors&gt; &lt;behavior name="WebBehavior"&gt; &lt;webHttp/&gt; &lt;/behavior&gt; &lt;/endpointBehaviors&gt; &lt;/behaviors&gt; &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt; &lt;/system.serviceModel&gt; &lt;system.webServer&gt; &lt;modules runAllManagedModulesForAllRequests="true"/&gt; &lt;/system.webServer&gt; &lt;/configuration&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload