posting items from asp.net website to craigslist - asp.net

here an example of posting to craigslist . http://www.craigslist.org/about/bulk_posting_interface in perl language.
any craigslist posting example in asp.net ?
I converted the perl example (from above link ) into asp.net but still generates an error , error is " The server committed a protocol violation. Section=ResponseStatusLine "
anyone have an idea about this. thanks in advance
here is my code ,
string xml = #"<?xml version='1.0'?><rdf:RDF xmlns='http://purl.org/rss/1.0/' xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' xmlns:cl='http://www.craigslist.org/about/cl-bulk-ns/1.0'> <channel><items><rdf:li rdf:resource='NYCBrokerHousingSample1'/><rdf:li rdf:resource='NYCBrokerHousingSample2'/> </items> <cl:auth username='fawad_ashfaq#hotmail.com' password='*******' accountID='14'/> </channel> <item rdf:about='NYCBrokerHousingSample1'> <cl:category>fee</cl:category> <cl:area>nyc</cl:area> <cl:subarea>mnh</cl:subarea> <cl:neighborhood>Upper West Side</cl:neighborhood> <cl:housingInfo price='1450' bedrooms='0' sqft='600'/> <cl:replyEmail privacy='C'>bulkuser#bulkposterz.net</cl:replyEmail> <cl:brokerInfo companyName='Joe Sample and Associates' feeDisclosure='fee disclosure here' /> <title>Spacious Sunny Studio in Upper West Side</title> <description><![CDATA[ posting body here ]]></description> </item> </rdf:RDF>";
string BASE_URL = "https://post.craigslist.org/bulk-rss/post";
HttpPost(BASE_URL, xml ); Private static string HttpPost(string url, string postData)
{
HttpWebRequest request = null;
Uri uri = new Uri(url);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
using (Stream writeStream = request.GetRequestStream())
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(postData);
writeStream.Write(bytes, 0, bytes.Length);
}
string result = string.Empty;
request.ProtocolVersion = System.Net.HttpVersion.Version10;
request.KeepAlive = false;
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (System.IO.StreamReader readStream = new System.IO.StreamReader(responseStream, Encoding.UTF8))
{
result = readStream.ReadToEnd();
}
}
}
}
catch (Exception exp)
{
// MessageBox.Show(exp.Message);
}
return result;
} // end HttpPost

you can use Fiddle2(windows) or Charles(os x) to capture http request and response, compare the difference between the perl example and asp.net example, then you may find the 'bug'.

I got around this by adding
request.ProtocolVersion = System.Net.HttpVersion.Version10;

Related

errors running HttpWebRequest.GetResponse - 401 unauthorized

I am using Lymbix client library for sentiment analysis.
When I run the code I am getting an error in (WebResponse)httpRequest.GetResponse(): 401-Unauthorized
(available at https://github.com/lymbix/.NET-Wrapper)
The function containing 401 error is given below:
private static string Post(string url, string data, List<string> headers)
{
HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpRequest.Method = "POST";
httpRequest.Accept = "application/json";
httpRequest.ContentType = "application/x-www-form-urlencoded";
if (headers != null)
{
foreach (string header in headers)
{
httpRequest.Headers.Add(header);
}
}
// write request?
byte[] postData = Encoding.UTF8.GetBytes(data.ToString());
httpRequest.ContentLength = postData.Length;
httpRequest.GetRequestStream().Write(postData, 0, postData.Length);
// read response
WebResponse webResponse = (WebResponse)httpRequest.GetResponse();
StreamReader webResponseStream = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8);
return webResponseStream.ReadToEnd();
}
It's saying you're not authorized, so you need to provide credentials.
HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpRequest.Method = "POST";
httpRequest.Accept = "application/json";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.Credentials = new NetworkCredential("username","password");
httpRequest.UseDefaultCredentials = false; //the default is false, but I included it here just to illustrate that it needs to be false in order to use the specified credentials

Google Drive api uploads file name as "Untitled"

I can upload file to google drive from my website, but my problem is it will show the file as Untitled after uploading.
How can I add or post title to the uploading file.
Thanks,
My Code:
public string UploadFile(string accessToken, byte[] file_data, string mime_type)
{
try
{
string result = "";
byte[] buffer = file_data;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/upload/drive/v2/files?uploadType=media");
request.Method = "POST";
request.ContentType = mime_type;
request.ContentLength = buffer.Length;
request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + accessToken);
var stream = request.GetRequestStream();
stream.Write(file_data, 0, file_data.Length);
stream.Close();
HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();//Get error here
if(webResponse.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = webResponse.GetResponseStream();
StreamReader responseStreamReader = new StreamReader(responseStream);
result = responseStreamReader.ReadToEnd();//parse token from result
var jLinq = JObject.Parse(result);
JObject jObject = JObject.Parse(jLinq.ToString());
webResponse.Close();
return jObject["alternateLink"].ToString();
}
return string.Empty;
}
catch
{
return string.Empty;
}
}
I used RestSharp for uploading a file to google drive.
public static void UploadFile(string accessToken, string parentId)
{
var client = new RestClient { BaseUrl = new Uri("https://www.googleapis.com/") };
var request = new RestRequest(string.Format("/upload/drive/v2/files?uploadType=multipart&access_token={0}", accessToken), Method.POST);
var bytes = File.ReadAllBytes(#"D:\mypdf.pdf");
var content = new { title = "mypdf.pdf", description = "mypdf.pdf", parents = new[] { new { id = parentId } }, mimeType = "application/pdf" };
var data = JsonConvert.SerializeObject(content);
request.AddFile("content", Encoding.UTF8.GetBytes(data), "content", "application/json; charset=utf-8");
request.AddFile("mypdf.pdf", bytes, "mypdf.pdf", "application/pdf");
var response = client.Execute(request);
if (response.StatusCode != HttpStatusCode.OK) throw new Exception("Unable to upload file to google drive");
}
Doing it with out using the google.apis dlls isnt that easy. You need to send the meta data before you send the rest of the file. For that you need to use uploadType=multipart
https://developers.google.com/drive/manage-uploads#multipart
This should get you started sorry its a wall of code. I havent had time to create a tutorial for this yet.
FileInfo info = new FileInfo(pFilename);
//Createing the MetaData to send
List<string> _postData = new List<string>();
_postData.Add("{");
_postData.Add("\"title\": \"" + info.Name + "\",");
_postData.Add("\"description\": \"Uploaded with SendToGoogleDrive\",");
_postData.Add("\"parents\": [{\"id\":\"" + pFolder + "\"}],");
_postData.Add("\"mimeType\": \"" + GetMimeType(pFilename).ToString() + "\"");
_postData.Add("}");
string postData = string.Join(" ", _postData.ToArray());
byte[] MetaDataByteArray = Encoding.UTF8.GetBytes(postData);
// creating the Data For the file
byte[] FileByteArray = System.IO.File.ReadAllBytes(pFilename);
string boundry = "foo_bar_baz";
string url = "https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart" + "&access_token=" + myAutentication.accessToken;
WebRequest request = WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "multipart/related; boundary=\"" + boundry + "\"";
// Wrighting Meta Data
string headerJson = string.Format("--{0}\r\nContent-Type: {1}\r\n\r\n",
boundry,
"application/json; charset=UTF-8");
string headerFile = string.Format("\r\n--{0}\r\nContent-Type: {1}\r\n\r\n",
boundry,
GetMimeType(pFilename).ToString());
string footer = "\r\n--" + boundry + "--\r\n";
int headerLenght = headerJson.Length + headerFile.Length + footer.Length;
request.ContentLength = MetaDataByteArray.Length + FileByteArray.Length + headerLenght;
Stream dataStream = request.GetRequestStream();
dataStream.Write(Encoding.UTF8.GetBytes(headerJson), 0, Encoding.UTF8.GetByteCount(headerJson)); // write the MetaData ContentType
dataStream.Write(MetaDataByteArray, 0, MetaDataByteArray.Length); // write the MetaData
dataStream.Write(Encoding.UTF8.GetBytes(headerFile), 0, Encoding.UTF8.GetByteCount(headerFile)); // write the File ContentType
dataStream.Write(FileByteArray, 0, FileByteArray.Length); // write the file
// Add the end of the request. Start with a newline
dataStream.Write(Encoding.UTF8.GetBytes(footer), 0, Encoding.UTF8.GetByteCount(footer));
dataStream.Close();
try
{
WebResponse response = request.GetResponse();
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
//Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
}
catch (Exception ex)
{
return "Exception uploading file: uploading file." + ex.Message;
}
If you need any explinations beyond the comments let me know. I strugled to get this working for a month. Its almost as bad as resumable upload.
I was searching for the solution of the given problem and previously I was putting uploadType=resumable that causes the given issue and when I used uploadType=multipart problem is resolved...

Web API Login with Cookie

I have an ASP.Net Web API and the documentation states I need to save an Auth Token to a cookie then pass it back for API requests. I can get the Auth Token without a problem. My question is what is the best way to save the cookie and send it back in the request.
I create a cookie in the RequestMessage, but I cannot find a way to send it back when making a request against the API. How do I preserve the state of the Login/cookie.
Any help is greatly appreciated, thanks.
Update
I am now able to obtain the cookie from the response. I am using this tutorial. http://www.asp.net/web-api/overview/working-with-http/http-cookies Let me point out if you want to use this tutorial make sure you update the Web API 4's code base. In the below method i am trying to simply, Login and Logout. However, I am receiving an Error Code 500.
public HttpWebResponse InitializeWebRequest()
{
//HttpResponseMessage logoutMessage = await Logout("bla");
string responseData = string.Empty;
string url = GetServerEndPoint();
string authToken = string.Empty;
string loginInstance = "https://example.com";
// Create request.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginInstance);
request.Method = "POST";
request.ContentType = "application/json";
request.CookieContainer = new CookieContainer();
HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result;
if (response.StatusCode == HttpStatusCode.OK)
{
using (System.IO.StreamReader responseReader = new System.IO.StreamReader(request.GetResponse().GetResponseStream()))
{
responseData = responseReader.ReadToEnd();
}
IList<string> authHeader = responseData.Split('{', '}').ToList();
authToken = authHeader[2].Substring(13, 25);
string sessionId = response.Headers.Get(8);
var nv = new NameValueCollection();
nv["sid"] = sessionId;
nv["token"] = authToken;
CookieHeaderValue cookieVal = new CookieHeaderValue("session", nv);
// Log out
string loginInstance2 = "https://example.com";
HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(loginInstance2);
request2.Method = "POST";
request2.ContentType = "application/json";
request2.Headers.Add(nv);
HttpWebResponse response2 = (HttpWebResponse)request2.GetResponseAsync().Result;
}
return response;
}
WOW WHAT A PAIN!
I have no idea why this took me so long to figure out, but after hours and hours and DAYs, of trying to get this stupid auth to work I finally figured it out. Here is the code.
One weird thing is I had to create the header format for the cookie. Which by definition isn't a true cookie, it is a damn header value. I had to create the header title, because when I extracted the JSON object from the file and converted it to string I was unable to keep the format in tact from the file.
public HttpWebResponse InitiliazeWebRequest()
{
string responseData = string.Empty;
string loginInstance = "url + logincreds";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginInstance);
request.Method = "POST";
request.ContentType = "application/json";
request.CookieContainer = new CookieContainer();
HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result;
if (response.StatusCode == HttpStatusCode.OK)
{
using (System.IO.StreamReader responseReader = new System.IO.StreamReader(request.GetResponse().GetResponseStream()))
{
responseData = responseReader.ReadToEnd();
}
var toke = response.Headers.Get("authToken");
JObject o = JObject.Parse(responseData);
_authToken = (string)o["response"]["authToken"].ToString();
return response;
}
return response;
}
public HttpWebResponse LogOut()
{
string responseData = string.Empty;
string loginInstance = "https://www.example.com/logout";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginInstance);
request.Method = "GET";
request.ContentType = "application/json";
request.Headers.Add("Cookie: authToken=" + _authToken);
HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result;
if (response.StatusCode == HttpStatusCode.OK)
{
using (System.IO.StreamReader responseReader = new System.IO.StreamReader(request.GetResponse().GetResponseStream()))
{
responseData = responseReader.ReadToEnd();
}
return response;
}
return response;
}

Request a page with server code

I need to request a series of pages and want to do from the server code as if you were doing with Ajax, I can do?, thanks
You're looking for the WebClient class.
Use this c# function. Add using System.Net; top of your page.
private string MakeWebRequest(string url) {
string retValue = String.Empty;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = null;
request.Method = "GET";
response = (HttpWebResponse)request.GetResponse();
StreamReader stReader = new StreamReader(response.GetResponseStream());
retValue = stReader.ReadToEnd();
stReader.Close();
response.Close();
stReader.Dispose();
stReader = null;
response = null;
request = null;
}
catch (Exception ex) {
}
return retValue;
}

Using Bit.ly API in ASP.NET 2.0

Hey I was wondering if anyone can point me to some example on how to use Bit.ly API in ASP.NET 2.0
I've done a really quick convert from an answer I found in VB.
I haven't tested this (sorry) but it may be of some help in the meantime, and I will sort it out to be a bit more C# style friendly.
public static string BitlyIt(string user, string apiKey, string strLongUrl)
{
StringBuilder uri = new StringBuilder("http://api.bit.ly/shorten?");
uri.Append("version=2.0.1");
uri.Append("&format=xml");
uri.Append("&longUrl=");
uri.Append(HttpUtility.UrlEncode(strLongUrl));
uri.Append("&login=");
uri.Append(HttpUtility.UrlEncode(user));
uri.Append("&apiKey=");
uri.Append(HttpUtility.UrlEncode(apiKey));
HttpWebRequest request = WebRequest.Create(uri.ToString()) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
request.ServicePoint.Expect100Continue = false;
request.ContentLength = 0;
WebResponse objResponse = request.GetResponse();
XmlDocument objXML = new XmlDocument();
objXML.Load(objResponse.GetResponseStream());
XmlNode nShortUrl = objXML.SelectSingleNode("//shortUrl");
return nShortUrl.InnerText;
}
Original code taken from here -
http://www.dougv.com/blog/2009/07/02/shortening-urls-with-the-bit-ly-api-via-asp-net/
I found the answer from tim and it's pretty solid. I needed a vb.net version so converted it back from C# - I figured this may help someone. It appears the the bit.ly link has changed; not sure if the version is necessary anymore; added a little error handling in case you pass in a bad url.
Public Shared Function BitlyIt(ByVal strLongUrl As String) As String
Dim uri As New StringBuilder("http://api.bitly.com/v3/shorten?")
'uri.Append("version=2.0.1") 'doesnt appear to be required
uri.Append("&format=xml")
uri.Append("&longUrl=")
uri.Append(HttpUtility.UrlEncode(strLongUrl))
uri.Append("&login=")
uri.Append(HttpUtility.UrlEncode(user))
uri.Append("&apiKey=")
uri.Append(HttpUtility.UrlEncode(apiKey))
Dim request As HttpWebRequest = TryCast(WebRequest.Create(uri.ToString()), HttpWebRequest)
request.Method = "GET"
request.ContentType = "application/x-www-form-urlencoded"
request.ServicePoint.Expect100Continue = False
request.ContentLength = 0
Dim objResponse As WebResponse = request.GetResponse()
Dim myXML As New StreamReader(objResponse.GetResponseStream())
Dim xr = XmlReader.Create(myXML)
Dim xdoc = XDocument.Load(xr)
If xdoc.Descendants("status_txt").Value = "OK" Then
Return xdoc.Descendants("url").Value
Else
Return "Error " & "ReturnValue: " & xdoc.Descendants("status_txt").Value
End If
End Function
there is a bit shorter version of BitlyIn
public static string BitlyEncrypt2(string user, string apiKey, string pUrl)
{
string uri = "http://api.bit.ly/shorten?version=2.0.1&format=txt" +
"&longUrl=" + HttpUtility.UrlEncode(pUrl) +
"&login=" + HttpUtility.UrlEncode(user) +
"&apiKey=" + HttpUtility.UrlEncode(apiKey);
HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
request.ServicePoint.Expect100Continue = false;
request.ContentLength = 0;
return (new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd());
}
Migrating from v3 to v4 of the Bitly API - Bitly V4 code for ASP.NET Applications
public string Shorten(string groupId, string token, string longUrl)
{
//string post = "{\"group_guid\": \"" + groupId + "\", \"long_url\": \"" + longUrl + "\"}";
string post = "{ \"long_url\": \"" + longUrl + "\"}";// If you've a free account.
string shortUrl = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api-ssl.bitly.com/v4/shorten");
try
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
request.ServicePoint.Expect100Continue = false;
request.Method = "POST";
request.ContentLength = post.Length;
request.ContentType = "application/json";
request.Headers.Add("Cache-Control", "no-cache");
request.Host = "api-ssl.bitly.com";
request.Headers.Add("Authorization", "Bearer " + token);
using (Stream requestStream = request.GetRequestStream())
{
byte[] postBuffer = Encoding.ASCII.GetBytes(post);
requestStream.Write(postBuffer, 0, postBuffer.Length);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader responseReader = new StreamReader(responseStream))
{
string json = responseReader.ReadToEnd();
shortUrl = Regex.Match(json, #"""link"": ?""(?[^,;]+)""").Groups["link"].Value;
//return Regex.Match(json, #"{""short_url"":""([^""]*)""[,}]").Groups[1].Value;
}
}
}
}
catch (Exception ex)
{
LogManager.WriteLog(ex.Message);
}
if (shortUrl.Length > 0) // this check is if your bit.ly rate exceeded
return shortUrl;
else
return longUrl;
}

Resources