Jump to content

Problems in using HttpWebRequest to interact with an API

- - - - -

  • Please log in to reply
No replies to this topic

#1
nutrione

nutrione

    Newbie

  • Members
  • Pip
  • 1 posts
hey there! my first post ever!
first of all I'm completely new to C# and .NET programming.

background: I'm trying to write a piece of code that uses HttpWebRequest to post an entry to a friendfeed feed via it's API. it's a console application and uses HTTP Basic Authentication for Credentials.

problem: when I run the application it throws an exception-> The remote server returned an error: (404) Not Found. this happens while it's working if you try it through your browser.

you can use FF's API Test Form to see that Basic Auth and all of this works and FF API does the job.

I'm using .NET 3.5/VS2008

here is the code I've written:

try

{

	HttpWebRequest mineReq = (HttpWebRequest)WebRequest.Create("http://friendfeed-api.com/v2/entry?");

	//http://friendfeed.com/api/share/ or http://friendfeed-api.com/v2/entry both can be used


	mineReq.KeepAlive = false;

	mineReq.ProtocolVersion = HttpVersion.Version10;


	mineReq.Credentials = new NetworkCredential("Username", "FFRemoteKey");

	mineReq.Method = "POST";

	String postData = "body=Hello API&format=xml";

	byte[] Databyte = Encoding.UTF8.GetBytes(HttpUtility.UrlEncode(postData));

	mineReq.ContentType = "multipart/form-data";

	mineReq.ContentLength = Databyte.Length;

	Stream dataStream = mineReq.GetRequestStream();

	dataStream.Write(Databyte, 0, Databyte.Length);

	dataStream.Close();


	WebResponse mineRes = mineReq.GetResponse();

	dataStream = mineRes.GetResponseStream();

	StreamReader reader = new StreamReader(dataStream);


	XmlDocument mineXML = new XmlDocument();

	mineXML.Load(reader.ReadToEnd());


	reader.Close();

	dataStream.Close();

	mineRes.Close();


	// XmlDocument readXML = new XmlDocument();

	// readXML.Load(mineReq.GetResponse().GetResponseStream());

}

catch(Exception exp)

{

	Console.WriteLine(exp.Message);

}

what am I doing wrong?
the attached file is the FF C# API Library written by FF API Team. it's for API version 1. but I'm going to use version 2, so I canot use it but I though it may help!

thanks in advance
P.S. I'm not a native English! so forgive me for any language errors!!!

Attached Files






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users