Hi can anyone please help I am currently writing an app for Twitter
and I seem to be having a problem with it with the error The remote
server returned an error: (401) Unauthorized. Is there anything I am
doing wrong.
Here is the code:
protected string ExecutePostCommand(string url, string userName,
string password, string data) {
WebRequest request = WebRequest.Create(url);
if (!string.IsNullOrEmpty(userName) &&
!string.IsNullOrEmpty
(password)) {
WebProxy pry = new WebProxy("10.5.83.2", 8080);
// pry.Credentials =
CredentialCache.DefaultCredentials;
pry.Credentials = new NetworkCredential("usernamel",
"password","domain");
request.Proxy = pry;
request.ContentType = "application/x-www-form-
urlencoded";
request.Method = "POST";
if (!string.IsNullOrEmpty(TwitterClient)) {
request.Headers.Add("X-Twitter-Client",
TwitterClient);
}
if
(!string.IsNullOrEmpty(TwitterClientVersion)) {
request.Headers.Add("X-Twitter-Version", TwitterClientVersion);
}
if (!string.IsNullOrEmpty(TwitterClientUrl)) {
request.Headers.Add("X-Twitter-URL",
TwitterClientUrl);
}
if (!string.IsNullOrEmpty(Source)) {
data += "&source=" +
HttpUtility.UrlEncode(Source);
}
byte[] bytes = Encoding.UTF8.GetBytes(data);
request.ContentLength = bytes.Length;
using (Stream requestStream =
request.GetRequestStream()) {
requestStream.Write(bytes, 0,
bytes.Length);
using (WebResponse response =
request.GetResponse()) {
using (StreamReader reader =
new StreamReader
(response.GetResponseStream())) {
return
reader.ReadToEnd();
}
}
}
}
return null;
}