Making a call to the online service form a dektop application (.net 2.0)
I have tried using the following code to add a new entry to the datastore
WebRequest req = null;
HttpWebResponse res = null;
// Initialise the web request
// Get Bytes for auth string {user}:{password}
ASCIIEncoding encoding = new ASCIIEncoding(); Byte[] userPassBytes = encoding.GetBytes(String.Format("{0}:{1}", "myusername", "mypassword"));
// Encode auth string to Base64
String authHeader = "Authorization: Basic " + Convert.ToBase64String(userPassBytes);
req = (HttpWebRequest)HttpWebRequest.Create(url);
req.ContentLength = encoding.GetBytes(content).Length;
req.Headers.Add(authHeader);
req.Method = "POST";
req.Timeout = HttpTimeout;
req.ContentType = "text/xml";
try
{
res = (HttpWebResponse)req.GetResponse();
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
HttpWebResponse res2 = (HttpWebResponse)ex.Response;
if (res2 != null)
{
throw new Exception();
}
}
throw new Exception();
}
But I always get internal error (500).
If i use the web tool, the information is updated correctly... I guess this has to do something with the authorization keypair but am not sure what... Any Ideas what might be wrong?
Thanks,
M. Vuksanovic
If you could provide the error test we may be able to figure out what is going on (on the WebException you still have access to the response and you can get to the response stream from there to get the response body).
Where do write the actual content to the request stream? I don't see that in your code sample.
As a side note, there are several things in the code snippet from yoru post that are actually already supported by the .NET API and you don't need to do by hand. Specifically:
-ContentLength: no need to set this, just put the content itself there and the HTTP stack will compute the lenght
-Authentication: check out the Credentials property in WebRequest. It handles credentials (including doing basic auth and other auth schemes) automatically for you.
Pablo Castro
Technical Lead
Microsoft Corporation
http://blogs.msdn.com/pablo
Please try the following code snippet. It makes use the httpwebrequest credentials objects to make handling authentication easier:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace WebTest
{
class Program
{
static void Main(string[] args)
{
HttpWebRequest req;
HttpWebResponse resp;
try
{
req = WebRequest.Create(http://servicename.rse/users/yourservice/yourservice.rse) as HttpWebRequest;
req.Method = "POST";
req.ContentType = "text/xml";
req.Credentials = new NetworkCredential("yourusername", "yourpassword");
string content = "<DataService><Customers><name>testname2</name></Customers></DataService>";
UTF8Encoding encoding = new UTF8Encoding();
byte[] postBytes = encoding.GetBytes(content);
req.ContentLength = postBytes.Length;
Stream s = req.GetRequestStream();
s.Write(postBytes, 0, postBytes.Length);
s.Close();
resp = req.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(resp.GetResponseStream());
Console.WriteLine(reader.ReadToEnd());
resp.Close();
}
catch (WebException e)
{
StreamReader reader = new StreamReader(e.Response.GetResponseStream());
Console.WriteLine(reader.ReadToEnd());
reader.Close();
}
Console.ReadKey();
}
}
}
Mike, thanks so much for posting this code.
Could you "please" post some more simple examples for us weak minded coders who are new to the System.Net namespace and its details.
Some very simple examples like:
authenticate
getstuff
post stuff
delete stuff
That would make my learning curve go alot faster.
Thanks in Advance,
Terrence