C# - vBulletin new thread - c#

i tried this:
public static void CreateNewThread(string url,string fId, string title, string message, string tag)
{
url += "newthread.php?do=postthread";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
//string result = "";
string values = "subject=" + title
+ "&message=" + message
+ "&tag=" + tag
+ "&do=postthread"
+ "&f=" + fId
+ "&s="
+ ""
;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = values.Length;
ServicePointManager.Expect100Continue = false; // prevents 417 error
using (StreamWriter writer = new StreamWriter(req.GetRequestStream(), Encoding.UTF8))
{
writer.Write(values);
}
HttpWebResponse c = (HttpWebResponse)req.GetResponse();
}
But this is doesnt work!

Try encoding the subject and message paramaters:
HttpUtility.UrlEncode(
string values = "subject=" + HttpUtility.UrlEncode(title)
+ "&message=" + HttpUtility.UrlEncode(message)
+ "&tag=" + HttpUtility.UrlEncode(tag)
+ "&do=postthread"
+ "&f=" + fId
+ "&s="
+ ""
;

Related

C# viewstate and eventvalidation not working

I a trying to web scraping from http://hydro.imd.gov.in/hydrometweb/(S(e4xftrnoogdzzz55q2l23i55))/Argaws.aspx. I am novice web-scraper in web pages with ViewState-generator and event validation. So far I have tried with the following code and it is returning the same page instead of redirecting to new page. Any help regarding it will be highly appreciated.
string link = "http://hydro.imd.gov.in/hydrometweb/(S(e4xftrnoogdzzz55q2l23i55))/Argaws.aspx";
WebClient client = new WebClient();
string intiHtml = client.DownloadString(link);
string viewStateNameDelimiter = "__VIEWSTATE";
string valueDelimiter = "value=\"";
int viewStateNamePosition = intiHtml.IndexOf(viewStateNameDelimiter);
int viewStateValuePosition = intiHtml.IndexOf(valueDelimiter, viewStateNamePosition);
int viewStateStartPosition = viewStateValuePosition + valueDelimiter.Length;
int viewStateEndPosition = intiHtml.IndexOf("\"", viewStateStartPosition);
string viewstatePos = intiHtml.Substring(viewStateStartPosition, viewStateEndPosition - viewStateStartPosition);
Console.WriteLine(viewstatePos);
Console.ReadKey();
string eventValidationNameDelimiter = "__EVENTVALIDATION";
int eventValidationNamePosition = intiHtml.IndexOf(eventValidationNameDelimiter);
int eventValidationValuePosition = intiHtml.IndexOf(valueDelimiter, eventValidationNamePosition);
int eventValidationStartPosition = eventValidationValuePosition + valueDelimiter.Length;
int eventValidationEndPosition = intiHtml.IndexOf("\"", eventValidationStartPosition);
string eventvalidate = intiHtml.Substring(eventValidationStartPosition, eventValidationEndPosition - eventValidationStartPosition);
Console.WriteLine(eventvalidate);
Console.ReadKey();
string viewgeneratorNameDelimiter = "__VIEWSTATEGENERATOR";
int viewgeneratorNamePosition = intiHtml.IndexOf(viewgeneratorNameDelimiter);
int viewgeneratorValuePosition = intiHtml.IndexOf(valueDelimiter, viewgeneratorNamePosition);
int viewgeneratorStartPosition = viewgeneratorValuePosition + valueDelimiter.Length;
int viewgeneratorEndPosition = intiHtml.IndexOf("\"", viewgeneratorStartPosition);
string viewgenerator = intiHtml.Substring(viewgeneratorStartPosition, viewgeneratorEndPosition - viewgeneratorStartPosition);
Console.WriteLine(viewgenerator);
Console.ReadKey();
string postData = viewStateNameDelimiter + "=" + viewstatePos + "&" + viewgeneratorNameDelimiter + "=" + viewgenerator + "&" + eventValidationNameDelimiter + "=" + eventvalidate + "&DropDownList2=17-01-2018&DropDownList1=07&GoBtn=Go";
Console.WriteLine(postData);
WebRequest req = WebRequest.Create(link);
//string postData = "";
//postData = "";
byte[] send = Encoding.Default.GetBytes(postData);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = send.Length;
Stream sout = req.GetRequestStream();
sout.Write(send, 0, send.Length);
sout.Flush();
sout.Close();
WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string returnvalue = sr.ReadToEnd();
Console.Write(returnvalue);
Console.ReadKey();

Twitter OAuth request_token stopped working

Out of no where, my twitter API calls, specificly my first step in the 3-legged auth, stopped working. I've compared the timestamps, keys and everything with the OAuth signature generator tool, and they all match (execpt oauth_nonce but thats the point I guess). Here is my code. Any suggestions or small observations would be appreciated.
protected void RequestToken()
{
string oauthcallback = Request.Url.Host + "/TwitterCallback.aspx";
string oauthconsumerkey = "xxx-consumerkey";
string oauthconsumersecret = "xxx-consumerSecret";
string oauthtokensecret = string.Empty;
string oauthtoken = string.Empty;
string oauthsignaturemethod = "HMAC-SHA1";
string oauthversion = "1.0";
string oauthnonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
TimeSpan timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
string oauthtimestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
string url = "https://api.twitter.com/oauth/request_token?oauth_callback=" + oauthcallback;
SortedDictionary<string, string> basestringParameters = new SortedDictionary<string, string>();
basestringParameters.Add("oauth_version", oauthversion);
basestringParameters.Add("oauth_consumer_key", oauthconsumerkey);
basestringParameters.Add("oauth_nonce", oauthnonce);
basestringParameters.Add("oauth_signature_method", oauthsignaturemethod);
basestringParameters.Add("oauth_timestamp", oauthtimestamp);
basestringParameters.Add("oauth_callback", Uri.EscapeDataString(oauthcallback));
//Build the signature string
string baseString = String.Empty;
baseString += "POST" + "&";
baseString += Uri.EscapeDataString(url.Split('?')[0]) + "&";
foreach (KeyValuePair<string, string> entry in basestringParameters)
{
baseString += Uri.EscapeDataString(entry.Key + "=" + entry.Value + "&");
}
//Remove the trailing ambersand char last 3 chars - %26
//baseString = baseString.Substring(0, baseString.Length - 3);
//Build the signing key
string signingKey = Uri.EscapeDataString(oauthconsumersecret) +
"&" + Uri.EscapeDataString(oauthtokensecret);
//Sign the request
HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey));
string oauthsignature = Convert.ToBase64String(
hasher.ComputeHash(new ASCIIEncoding().GetBytes(baseString)));
//Tell Twitter we don't do the 100 continue thing
ServicePointManager.Expect100Continue = false;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(#url);
string authorizationHeaderParams = String.Empty;
authorizationHeaderParams += "OAuth ";
authorizationHeaderParams += "oauth_nonce=" + "\"" +
Uri.EscapeDataString(oauthnonce) + "\",";
authorizationHeaderParams += "oauth_signature_method=" + "\"" +
Uri.EscapeDataString(oauthsignaturemethod) + "\",";
authorizationHeaderParams += "oauth_timestamp=" + "\"" +
Uri.EscapeDataString(oauthtimestamp) + "\",";
authorizationHeaderParams += "oauth_consumer_key=" + "\"" +
Uri.EscapeDataString(oauthconsumerkey) + "\",";
authorizationHeaderParams += "oauth_signature=" + "\"" +
Uri.EscapeDataString(oauthsignature) + "\",";
authorizationHeaderParams += "oauth_version=" + "\"" +
Uri.EscapeDataString(oauthversion) + "\"";
webRequest.Headers.Add("Authorization", authorizationHeaderParams);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
//Allow us a reasonable timeout in case Twitter's busy
webRequest.Timeout = 3 * 60 * 1000;
try
{
HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
Stream dataStream = webResponse.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
var uri = new Uri("https://test.dk?" + responseFromServer);
var token = HttpUtility.ParseQueryString(uri.Query).Get("oauth_token"); ;
var tokensecret = HttpUtility.ParseQueryString(uri.Query).Get("oauth_token_secret");
Response.Write(responseFromServer);
Response.Redirect("https://api.twitter.com/oauth/authorize?force_login=true&oauth_token=" + token);
}
catch (Exception ex)
{
Response.Write(ex.GetBaseException());
}
}
The error obviously happens when I do the HTTP request webRequest.GetResponse()
It returns a 401 unauthorized
Apperently you have to include the oauth version number in the URL now, or else it will fall back to the oldest version (or maybe the newest, can't remember).
Providing /oath/1.0/ or /1.0/oauth/ or what ever solved my issue as i recall it (it's been a while).

not getting the SOAP response

My SOAP request works - hits the server and displays my request in the server logs and sends the response but I am unable to read the response with my codes (C# & ASP.NET) below.Am I missing something?
try
{
string url = "http://a.b.c.d/ABC/services/DOMFlightAvailability?wsdl";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.KeepAlive = false;
req.ProtocolVersion = HttpVersion.Version10;
req.Method = "POST";
string request = "<Request>"
+ "<Origin>BOM</Origin>"
+ "<Destination>BLR</Destination>"
+ "<DepartDate>2014-11-15</DepartDate>"
+ "<ReturnDate>2014-11-15</ReturnDate>"
+ "<AdultPax>1</AdultPax>"
+ "<ChildPax>1</ChildPax>"
+ "<InfantPax>1</InfantPax>"
+ "<Currency>INR</Currency>"
+ "<Clientid>123</Clientid>"
+ "<Clientpassword>123</Clientpassword>"
+ "<Clienttype>123</Clienttype>"
+ "<Preferredclass>E</Preferredclass>"
+ "<mode>ONE</mode>"
+ "<PreferredAirline>AI,G8,IC,6E,9W,S2,IT,9H,I7,SG</PreferredAirline>"
+ "</Request>";
byte[] postBytes = Encoding.ASCII.GetBytes("xmlRequest=" + request + "");
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = postBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
Stream resStream = response.GetResponseStream();
var sr = new StreamReader(response.GetResponseStream());
string responseText = sr.ReadToEnd();
Response.Write(responseText);
lblResult.Text = responseText;
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
This is my other way of doing it - hits the server and trasmits the response but I am still unable to get the response data
try
{
DOMFlightAvailabilityPortTypeClient obj = new DOMFlightAvailabilityPortTypeClient();
string req = "<Request>"
+ "<Origin>BOM</Origin>"
+ "<Destination>DEL</Destination>"
+ "<DepartDate>2014-11-20</DepartDate>"
+ "<ReturnDate>2014-12-21</ReturnDate>"
+ "<AdultPax>1</AdultPax>"
+ "<ChildPax>0</ChildPax>"
+ "<InfantPax>0</InfantPax>"
+ "<Currency>INR</Currency>"
+ "<Clientid>123</Clientid>"
+ "<Clientpassword>123</Clientpassword>"
+ "<Clienttype>123</Clienttype>"
+ "<Preferredclass>E</Preferredclass>"
+ "<mode>ONE</mode>"
+ "<PreferredAirline>AI,G8,IC,6E,9W,S2,IT,9H,I7,SG</PreferredAirline>"
+ "</Request>";
string str = obj.getAvailability(req);
XmlDocument doc = new XmlDocument();
doc.LoadXml(str);
DataSet ds = new DataSet();
ds.ReadXml(new XmlTextReader(new StringReader(str)));
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}

Java vs C# HTTP request with JSON data

Im trying to convert Java program to C#. This programe sent JSON object to server using a HTTP POST. Java program works fine. return 200. But C# program return 400 (bad request). What can be the cause
Java Code
String base_url = "https://test-url.com";
String username = "test-user";
String password = "test-pass";
String client_id = "test-client";
String client_secret = "test-key";
String loginUrl = base_url + "session/login";
Charset utf8 = Charset.forName("UTF-8");
ContentType jason_content_type = ContentType.create("application/json", utf8);
try {
HttpClient c = HttpClients.custom().setUserAgent(client_id + "/1.0").build();
HttpPost p = new HttpPost(loginUrl);
String json_str = "{" + "\"userId\":\"" + username + "\"," + "\"password\":\"" + password + "\"," + "\"clientId\":\"" + client_id + "\"," + "\"clientSecret\":\"" + client_secret + "\"" + "}";
p.setEntity(new StringEntity(json_str, jason_content_type));
HttpResponse r = c.execute(p);
int status = r.getStatusLine().getStatusCode();
} catch (IOException e) {
System.out.println(e);
}
C# Code
string base_url = "https://test-url.com";
string username = "test-user";
string password = "test-pass";
string client_id = "test-client";
string client_secret = "test-key";
string login_url = base_url + "session/login";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(login_url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = WebRequestMethods.Http.Post;
httpWebRequest.UserAgent = client_id + "/1.0";
httpWebRequest.ProtocolVersion=HttpVersion.Version11;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream(), Encoding.UTF8))
{
string json_str = "{" + "\"userId\":\"" + username + "\"," + "\"password\":\"" + password + "\"," + "\"clientId\":\"" + client_id + "\"," + "\"clientSecret\":\"" + client_secret + "\"" + "}";
streamWriter.Write(json_str);
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
Try out adding in C# :
httpWebRequest.ContentType = "application/x-www-form-urlencoded";

FtpWebRequest returns "The remote server returned an error: (530) Not logged in"

I get this error "The remote server returned an error: (530) Not logged in." while uploading the file using FtpWebRequest.
Error occurs only when I transfer the files to path having subfolders, otherwise it perfectly works fine.
Uploading large files about 5 to 10 MB, it gets timed out.
void FtpTransfer(string siteName, string portNumber, string ftpUser, string ftpPassword, string destPath)
{
FtpWebRequest request;
DateTime now = DateTime.Now;
string now_string =
(now.Year).ToString()
+ "_" +
(now.Month).ToString("0#")
+ "_" +
(now.Day).ToString("0#");
foreach (object item in listBox1.Items)
{
string srcFile = item.ToString();
lblSource.Text = srcFile;
Uri uri = new Uri(srcFile);
string destFile = srcFile.Replace(lblPath.Text, "").Replace("\\\\", "\\").Replace("\\", "/").Replace("www/","");
Configuration oConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
int timeout = int.Parse(oConfig.AppSettings.Settings["TimeOut"].Value);
if (siteName == "mysite1.co.in" || siteName == "sd1.mysite2.net")
destFile = "ftp://" + siteName + ":" + portNumber + "/" + siteName + "/_test" + destFile; //error here
else
destFile = "ftp://" + siteName + ":" + portNumber + "/" + siteName + destFile; //no error
lblDestn.Text = destFile;
request = (FtpWebRequest)WebRequest.Create(destFile);
request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
request.Timeout = 6000;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = true;
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(#srcFile);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
string path = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
System.IO.StreamWriter w = System.IO.File.AppendText(path + "\\log_" + now_string + ".txt");
w.WriteLine(DateTime.Now.ToString("yyy-MM-dd HH:mm:ss")
+ " "
+ srcFile
+ " "
+ destFile
+ " "
+ response.StatusDescription);
w.Close();
response.Close();
}
I am assuming that you have already attempted to do the same operation through a standard FTP client. If not, try that. I would then use Wireshark to make sure that is the response coming from the server and verify that the credentails are being sent. After that is verified, check with the FTP owner and make sure the server is configured appropriately.
I do not exactly know the solution for your problem. but some suggestions:
Use using(...) on IDisposable classes when ever possible. This promotes proper resource releasing and cleanup when you are finished with it. MSDN: Using
You use a timeout of 6000 miliseconds, may you should increase it for huge files or use your local variable timeout (read from you app settings).
Improved code with using:
private void FtpTransfer(string siteName, string portNumber, string ftpUser, string ftpPassword, string destPath)
{
DateTime now = DateTime.Now;
string now_string =
(now.Year).ToString()
+ "_" +
(now.Month).ToString("0#")
+ "_" +
(now.Day).ToString("0#");
foreach (object item in listBox1.Items)
{
string srcFile = item.ToString();
lblSource.Text = srcFile;
Uri uri = new Uri(srcFile);
string destFile = srcFile.Replace(lblPath.Text, "").Replace("\\\\", "\\").Replace("\\", "/").Replace("www/", "");
Configuration oConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
int timeout = int.Parse(oConfig.AppSettings.Settings["TimeOut"].Value);
if (siteName == "mysite1.co.in" || siteName == "sd1.mysite2.net")
destFile = "ftp://" + siteName + ":" + portNumber + "/" + siteName + "/_test" + destFile; //error here
else
destFile = "ftp://" + siteName + ":" + portNumber + "/" + siteName + destFile; //no error
lblDestn.Text = destFile;
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(destFile);
request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
request.Timeout = 6000;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = true;
// Copy the contents of the file to the request stream.
byte[] fileContents;
using (StreamReader sourceStream = new StreamReader(#srcFile))
{
fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
}
request.ContentLength = fileContents.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileContents, 0, fileContents.Length);
}
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
string path = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
System.IO.StreamWriter w = System.IO.File.AppendText(path + "\\log_" + now_string + ".txt");
w.WriteLine(DateTime.Now.ToString("yyy-MM-dd HH:mm:ss")
+ " "
+ srcFile
+ " "
+ destFile
+ " "
+ response.StatusDescription);
}
}
}

Categories