How do I create an ATOM client in Csharp?

  • Thread starter Thread starter Anonieko Ramos
  • Start date Start date
A

Anonieko Ramos

Blogger has posted this code:


===============================
using System;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web.Services.Protocols;
using System.Xml;

public class AtomClient {
public static void Main(string[] arg) {
if (arg.Length != 3) {
Console.WriteLine("Usage: AtomClient user password url");
return;
}

string user = arg[0];
string password = arg[1];
string url = arg[2];

// Create a blog entry
entryType blog = new entryType();
blog.issued = DateTime.Now;
blog.title = "First Post!";

// Author
blog.author = new authorType();
blog.author.name = "Sam Ruby";
blog.author.url = "http://www.intertwingly.net/blog/";

// Generator
blog.generator = new generatorType();
blog.generator.url = "http://www.intertwingly.net/blog/";
blog.generator.Value = "WSDL C# Client";

// Fill in the content
XmlDocument d = new XmlDocument();
d.InnerXml = "<div xmlns='http://www.w3.org/1999/xhtml'>Hello World!</div>";
blog.content = new contentType[1];
blog.content[0] = new contentType();
blog.content[0].mode = contentTypeMode.xml;
blog.content[0].type = "application/xhtml+xml";
blog.content[0].Any = new XmlNode[1];
blog.content[0].Any[0] = d.DocumentElement;

// Create an 'atom' authorization header
Security auth = new Security();
UsernameToken unt = new UsernameToken();
auth.UsernameToken = new UsernameToken();
auth.UsernameToken.Nonce = new Random().Next().ToString();
auth.UsernameToken.Username=user;
auth.UsernameToken.Created=DateTime.Now.ToString("u").Replace(' ','T');

// Fill in the password
SHA1 md = new SHA1CryptoServiceProvider();
string v = auth.UsernameToken.Nonce + auth.UsernameToken.Created + password;
byte[] digest = md.ComputeHash(Encoding.Default.GetBytes(v));
auth.UsernameToken.Password=new passwordType();
auth.UsernameToken.Password.Value=Convert.ToBase64String(digest);
auth.UsernameToken.Password.Type="wsse:PasswordDigest";
auth.UsernameToken.Password.AnyAttr=new XmlAttribute[1];
auth.UsernameToken.Password.AnyAttr[0]=d.CreateAttribute("wsse:placeholder",
"http://schemas.xmlsoap.org/ws/2002/07/secext");

try {
// Post the request
AtomAPI api = new AtomAPI();
api.SecurityValue = auth;
api.Url = url;
api.POST(ref blog);
} catch (SoapException fault) {
System.Console.WriteLine(fault.Message);
} catch (WebException httpStatus) {
System.Console.WriteLine(httpStatus.Message);
}

}
}
 
Hello, Anonieko!

==========================================================================
public class AtomClient {
public static void Main(string[] arg) {
if (arg.Length != 3) {
Console.WriteLine("Usage: AtomClient user password url");
return;
}

string user = arg[0];
string password = arg[1];
string url = arg[2];
..
..
..
====================================================================================
Well, first of all, I hope that doesn't compile:
arg is local variable (actially, parameter) for the "main()" method
and the "main()" method shouldn't start with a capital letter
(main()!=Main())

If I understood you correctly, you should create 'user', 'password', and
'url' varibles
separately from the "main()" method (just like you did), but do the
assignment inside it (see code below).
I prefer creating the variables BEFORE all methods even though it is not
required,
as long as it is declared within the class, just makes code look better; I
also prefer
EXPLICITLY declaring variables as 'private', 'public', 'static' (if
necessary), etc
keywords and then call them as "this.<variablename>" or
"<classname>.<variablename>"
for static variables (same applies for methods) - again makes code look
better, at least to me...

====================================================================================
public class AtomClient {
private string
user="",
password="",
url="";
//initialization was not really required for this matter - see "main()"
method:

public static void main(string[] arg)
{
if (arg.Length != 3)
{
Console.WriteLine("Usage: AtomClient user password url");
Application.Exit();
}
else
{
this.user = arg[0];
this.password = arg[1];
this.url = arg[2];
}
}
//Here goes the rest of your code. Sorry, I didn't check it...
..
..
..
}
==========================================================================

Hope this helps... :):):)
With best regards, Nurchi BECHED.
 
Nurchi said:
Hello, Anonieko!

==========================================================================
public class AtomClient {
public static void Main(string[] arg) {
if (arg.Length != 3) {
Console.WriteLine("Usage: AtomClient user password url");
return;
}

string user = arg[0];
string password = arg[1];
string url = arg[2];
.
.
.
============================================================================
========
Well, first of all, I hope that doesn't compile:
arg is local variable (actially, parameter) for the "main()" method
and the "main()" method shouldn't start with a capital letter
(main()!=Main())

This is C#, not Java or C++. We're doing Main() here ;-)
 
Back
Top