HttpWebRequest
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: HttpWebRequest

Hybrid View

  1. #1
    Join Date
    Nov 2010
    Posts
    9

    Question HttpWebRequest

    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:
    Code:
    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 Attached Files

  2. #2
    Join Date
    Nov 2010
    Posts
    9

    Re: HttpWebRequest

    by the way, if I try mineReq.ProtocolVersion = HttpVersion.Version11 it will throw an exception-> The server committed a protocol violation. Section=ResponseStatusLine

  3. #3
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: HttpWebRequest

    you sample code is incomplete and it cannot be tested (the HttpUtility is missing). I also get a "The remote server returned an error: (401) Unauthorized." exception. isn't your firewall blocking your app?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  4. #4
    Join Date
    Nov 2010
    Posts
    9

    Re: HttpWebRequest

    the code is not incomplete. you need to add web ref to your Visual Studio project to make HttpUtulity work. "using System.Web" is not enough.

    and no, my firewall setting is fine.

    if you get that error, that means the code runs fine you, that's great!
    did you try to enter a valid username and a valid remote key of friendfeed?
    will you do that and tell me the results?
    thanks in advance

  5. #5
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: HttpWebRequest

    Quote Originally Posted by nutrione View Post
    the code is not incomplete. you need to add web ref to your Visual Studio project to make HttpUtulity work. "using System.Web" is not enough.
    and you are not about to tell me what I should add?

    Quote Originally Posted by nutrione View Post
    did you try to enter a valid username and a valid remote key of friendfeed?
    where do I find a valid user and pass?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  6. #6
    Join Date
    Nov 2010
    Posts
    9

    Re: HttpWebRequest

    dude! I just told you

    open up your Visual Studio project, go to solution explorer, right click on refference folder, click on "Add Refference", when a window appears, on ".NET" tab, search for "System.Web" and select it and click on ok, you're done.

    what do you mean where? Friendfeed.com. go there, register, find your Remote Key (after you login, visit friendfeed.com/remotekey) and enter them in the code above on line 5: ("Username", "FFRemoteKey")
    **Remenber your Remote Key is a private thing! like a password. don't post it here.

    then run the project, and see what heppens. if you don't get any errors, check your friendfeed account to see that the text "Hello API" is posted on your feed or not.
    Last edited by nutrione; December 8th, 2010 at 04:32 PM.

  7. #7
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: HttpWebRequest

    Quote Originally Posted by nutrione View Post
    dude! I just told you

    open up your Visual Studio project, go to solution explorer, right click on refference folder, click on "Add Refference", when a window appears, on ".NET" tab, search for "System.Web" and select it and click on ok, you're done.
    sure, I already tried it but I don't see any System.Web dll to add. I added a using System.Web but VS still does not recognize HttpUtility and even IntelliSense does not see it even though it's there according to msdn

    Quote Originally Posted by nutrione View Post
    what do you mean where? Friendfeed.com. go there, register, find your Remote Key (after you login, visit friendfeed.com/remotekey) and enter them in the code above on line 5: ("Username", "FFRemoteKey")
    **Remenber your Remote Key is a private thing! like a password. don't post it here.
    ok, I'll try it later. I just thought friendfeed is some of your projects and did not do further research
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  8. #8
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: HttpWebRequest

    someone will laugh... but I don't know what the password is. could it be my facebook pass when I registered with my facebook account?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  9. #9
    Join Date
    Nov 2010
    Posts
    9

    Re: HttpWebRequest

    Hi!
    Oh no laughs. friendfeed can be a little confusing. click on settings on the right side of your avatar, on settings window, click on change password, enter something and save it. once more, go to settings, and enter a username for your account and save. now you have a username and a password to login with, directly to FF.

    Do a login, visit friendfeed.com/remotekey and get your Remote Key. then enter your username and key in the code above. and run the code. then go back to your FF account to see if "Hello API" is posted there.


    Thanks mate
    Last edited by nutrione; December 10th, 2010 at 08:29 PM.

  10. #10
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,000

    Re: HttpWebRequest

    Hope you aren't harvesting credentials...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  11. #11
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: HttpWebRequest

    Quote Originally Posted by dglienna View Post
    Hope you aren't harvesting credentials...
    I don't think so, though my first thought was exacly the same but then I read that wikipedia states that friendfeed is owned by facebook. besides I use fake accounts anyway
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  12. #12
    Join Date
    Nov 2010
    Posts
    9

    Re: HttpWebRequest

    Quote Originally Posted by dglienna View Post
    Hope you aren't harvesting credentials...
    are you serous?! WOW!
    dude, look at the first post. does it look like someone trying to "harvest credentials"?
    if he was familiar with all this FF thing, I would never type such long instructions just to setup and run a FF account!
    and yes, FF is owned by facebook.

    first no one answers, then when someone is trying to help....uh.

    @memeloo thanks buddy. I still get Error 404: Not Found. I can create an account and provide the Uname and Key for you, so you can test this, is that Ok? I don't wanna cause any troubles.

    P.S. @dglienna you know, I'm thinking how can I "harvest credentials", while I don't own the FF or anything that can lead memeloo's account details to me. where did you get the Idea?
    Last edited by nutrione; December 11th, 2010 at 07:21 AM.

  13. #13
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: HttpWebRequest

    ok, I did like you said. then I moved the "entry?" part to post data like this:
    Code:
    HttpWebRequest mineReq = (HttpWebRequest)WebRequest.Create("https://friendfeed-api.com/v2/");
    ...
    String postData = "entry?body=Hello API&format=xml";
    ...
    and got the 403 authentication error. I'll look at it agian later, now I must go. should you figure it out in the meantime please let me know. I'm anxious how it works

    (I also changed http to https which I prefer)
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  14. #14
    Join Date
    Nov 2010
    Posts
    9

    Re: HttpWebRequest

    the URL should be "https://friendfeed-api.com/v2/entry" because you are going to post an entry. for other things like posting a comment there is https://friendfeed-api.com/v2/comment and so on (you can find these URLs at http://friendfeed.com/api/documentation) so don't change the URL. it also has a format=xml becasue FF uses Json by default, but I'm going to use XML therefore I have to add this parameter.

    If you take a look at FF API Documentation, you'll see what's going on. and right now there is a problem with the API. this https://friendfeed-api.com/v2/entry?body=Hi&format=xml should post an entry. but FF returns an "ID required" error. according to the documentation, posting an entry does NOT require an ID. you can try that in your browser, it will ask you for your username and remoteKey.

    Also take a look at http://friendfeed.com/static/html/apitest.html it shows API usage in action.

    I don't know what's going on with FF API. they are changing some things. recently they just managed to fix the search feature. I think that's why the API acts odd!

    why anxious?! we are trying to talk to the Friendfedd API via a HttpWebRequest and we're not using OAuth, but we're using HTTP Basic Authentication. that's the main Idea. I mentioned it all in the first post why I'm not using OAuth? (I know its safer) because this is gonna be a mobile application, not a web (browser) app. since OAuth needs browser, I use remoteKey which Friendfeed provided for this use. it's a feature of FF. I don't know why you're anxious about it. but I think you don't have a similar programming experience. that's why this all looks Suspicious to you!!!! But after all, Friendfeed API Documentation is there. explains everything, I hope.

    P.S. If you're not comfortable with this, just drop it and let go. just don't do it. I appreciate your work and time you have spent.

    I feel like I talk too much sorry, but I felt it was Necessary.
    Last edited by nutrione; December 11th, 2010 at 09:12 AM.

  15. #15
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: HttpWebRequest

    interestingly it works when you put all the parameters in the call to Create the instance of HttpWebRequest like this:
    Code:
    HttpWebRequest httpWebRequest = 
       (HttpWebRequest)WebRequest.Create("http://friendfeed-api.com/v2/entry?body=Hello API&format=xml");
    and skips the line
    Code:
    Stream dataStream = httpWebRequest.GetRequestStream();
    "Hallo API" was successfuly added to my feed
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center