C# Github API Unable to create issue - c#

Im unable to create an issue on github even though its supposed to work. I do manage to get something back out of it for some reason.
What i am trying to do is uploading a string in json format that has the same feature as POST. Apparently that is not working.
using System;
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json;
namespace TestProject
{
class Program
{
static void Main(string[] args)
{
CreateGithubIssue("Test Issue - C#", "This is just a test to check if i can manage to create an issue from C#.");
}
static string username = "someUser";
static string password = "somePassword";
static string repoIssueLink = "http://api.github.com/repos/someUser/someRepo/issues";
public static void CreateGithubIssue(string Title, string Description)
{
WebClient webClient = new WebClient();
webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
webClient.Credentials = new NetworkCredential(username, password);
string jsonPost = JsonConvert.SerializeObject(new Issue(Title, Description), Formatting.Indented);
string response = webClient.UploadString(repoIssueLink, jsonPost);
Console.WriteLine(response);
Console.WriteLine(jsonPost);
}
}
public class Issue
{
public string title;
public string body;
public List<string> assignees;
public int milestone;
public List<string> labels;
public Issue(string title = "Default Title", string body = "Default Body", List<string> assignees = null, int milestone = 0, List<string> labels = null)
{
if (assignees == null) assignees = new List<string>();
if (labels == null) labels = new List<string>();
this.title = title;
this.body = body;
this.assignees = assignees;
this.milestone = milestone;
this.labels = labels;
}
}
}
The Output:

Alright, after asking some other people, i managed to find the answer.
I changed the link to HTTPS and used this code:
WebClient webClient = new WebClient();
webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));
webClient.Headers[HttpRequestHeader.Authorization] = string.Format("Basic {0}", credentials);
string jsonOutput = JsonConvert.SerializeObject(new Issue(Title, Description), Formatting.Indented);
string response = webClient.UploadString(repoIssueLink, jsonOutput);
Console.WriteLine(response);
Console.WriteLine(jsonOutput);

Related

Asp.net core FromBody always getting NULL

Hello friends i have created Asp.net core web service and calling this web service form web application. in this web api user upload pdf and document type and File number.
this is my asp.net core controller
[HttpPost ]
[Route("v1/ReceiveNoteData")]
public string ReceiveNoteData([FromBody] ReceiveNoteDataReq request)
{
return _IReceiveNoteData.ReceiveNoteDataResponse(request);
}
ReceiveNoteDataReq class
public class ReceiveNoteDataReq
{
public byte[] DocumentBody { get; set; }
public string DocType { get; set; }
public string FileNumber { get; set; }
}
when i am trying to call this web service using webrequest on service side frombody always getting NULL
if i am sending only file number and doc type i am getting data in from body but when i am trying to send pdf data from body getting NULL.
this is how i am calling the web service in my web application.
ReceiveNoteDataReq req = new ReceiveNoteDataReq()
req.DocumentBody = bytes;// pdf byte data
req.FileNumber = txtid.Text;
req.DocType = doctype;
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
string url = "http://localhost:905/v1/ReceiveNoteData";
CookieContainer cookieJar = new CookieContainer();
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
webRequest.MediaType = "application/json";
webRequest.Accept = "application/json";
webRequest.CookieContainer = cookieJar;
webRequest.KeepAlive = true;
webRequest.UserAgent = #"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36";
webRequest.Credentials = CredentialCache.DefaultCredentials;
StreamWriter writer = new StreamWriter(webRequest.GetRequestStream());
JavaScriptSerializer jss = new JavaScriptSerializer();
string yourdata = jss.Serialize(req); // here i am converting class to json string.
writer.Write(yourdata);
writer.Close();
var myWebResponse = webRequest.GetResponse();
var responseStream = myWebResponse.GetResponseStream();
if (responseStream != null)
{
var myStreamReader = new StreamReader(responseStream, Encoding.Default);
result = myStreamReader.ReadToEnd();
}
Thanks in advance.
when i am trying to call this web service using webrequest on service side frombody always getting NULL if i am sending only file number and doc type i am getting data in from body but when i am trying to send pdf data from body getting NULL.
If you'd like to post pdf data with other information from request body to you backend service, you can refer to the following example.
//...
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
string yourdata = System.Text.Json.JsonSerializer.Serialize(
new
{
DocType = doctype,
FileNumber = txtid.Text,
DocumentBody = base64String
});
writer.Write(yourdata);
writer.Close();
//...
The ByteArrayModelBinder can help bind data to byte[] DocumentBody property well.
public class ReceiveNoteDataReq
{
[ModelBinder(BinderType = typeof(ByteArrayModelBinder))]
public byte[] DocumentBody { get; set; }
public string DocType { get; set; }
public string FileNumber { get; set; }
}
Test Result
Besides, as #SabirHossain mentioned, you can try to modify your code to upload file through FormData, and this SO thread discussed similar requirement, you can refer to it.

Not able to download zip file using httpwebrequest through c# code. It gets downloaded through browser

I want to download the zip file from one of the website https://eqrreportviewer.ferc.gov/. The way in which the zip file gets downloaded is that you click on the filing inquiries tab first. In the reportType dropdown select SubmissionsBydate and in export dropdown select CSV. Now click on submit button and the zip file gets downloaded. I want to automate this process. I have written a code in C# by capturing the request along with its headers and passing that details to the site, but I am not able to download the file through code.
This is the code that I have written:
public static string PageSourceCode { get; set; }
//The ASP.NET SessionID to add validation to posts
public static string SessionID { get; set; }
//The value we are posting to the page on subsequent calls
public static string PostBackValue { get; set; }
public static string AcquisitionURL = "https://eqrreportviewer.ferc.gov";
static void Main(string[] args)
{
Acquire();
}
private static void Acquire()
{
GetLandingPage();
PopulatePostBackValueForSubmitBtn();
PostToPageForSubmitBtn();
}
private static void GetLandingPage()
{
string mainPageOutput = string.Empty;
HttpWebRequest objRequestLandingPage = (HttpWebRequest)WebRequest.Create(AcquisitionURL);
objRequestLandingPage.Method = WebRequestMethods.Http.Get;
objRequestLandingPage.Headers.Add("Cache-Control", "max-age=0");
objRequestLandingPage.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
objRequestLandingPage.Headers.Add("Accept-Encoding", "gzip, deflate, br");
objRequestLandingPage.Headers.Add("Accept-Language", "en-US,en;q=0.9");
objRequestLandingPage.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36";
objRequestLandingPage.Headers.Add("Sec-Fetch-Dest", "document");
objRequestLandingPage.Headers.Add("Sec-Fetch-Mode", "navigate");
objRequestLandingPage.Headers.Add("Sec-Fetch-Site", "none");
objRequestLandingPage.Headers.Add("Sec-Fetch-User", "?1");
objRequestLandingPage.Headers.Add("Upgrade-Insecure-Requests", "1");
//objRequestLandingPage.Headers.Add("Connection", "keep-alive");
objRequestLandingPage.KeepAlive = true;
objRequestLandingPage.Host = "eqrreportviewer.ferc.gov";
using (WebResponse objResponseLandingPage = objRequestLandingPage.GetResponse())
{
WebHeaderCollection headers = objResponseLandingPage.Headers;
using (Stream streamLandingPage = objResponseLandingPage.GetResponseStream())
using (StreamReader streamReaderLandingPage = new StreamReader(streamLandingPage))
{
mainPageOutput = streamReaderLandingPage.ReadToEnd();
}
SessionID = headers["Set-Cookie"];
}
SessionID = StripCookie(SessionID);
//Set the source code of the page
PageSourceCode = mainPageOutput;
}
private static void PopulatePostBackValueForSubmitBtn()
{
if (!String.IsNullOrEmpty(PageSourceCode))
{
// get fields from landing page
Dictionary<string, string> formFields = GetFormFields(PageSourceCode);
formFields["TabContainerReportViewer$TabPanelReporting$TabContainerReports$TabPanelSummaryReports$ddlReportTypeSum"] = "0";
formFields["TabContainerReportViewer$TabPanelReporting$TabContainerReports$TabPanelSummaryReports$ddlReportPeriodSum"] = "650";
formFields["TabContainerReportViewer$TabPanelReporting$TabContainerReports$TabPanelSummaryReports$ListSearchExtender1_ClientState"] = String.Empty;
formFields["TabContainerReportViewer$TabPanelReporting$TabContainerReports$TabPanelFilingInquiries$ddlReportType"] = "4";
formFields["TabContainerReportViewer$TabPanelReporting$TabContainerReports$TabPanelFilingInquiries$txtFromSubmissionDate"] = System.DateTime.Now.Date.AddDays(-30).ToShortDateString();
formFields["TabContainerReportViewer$TabPanelReporting$TabContainerReports$TabPanelFilingInquiries$txtToSubmissionDate"] = System.DateTime.Now.Date.ToShortDateString();
formFields["TabContainerReportViewer$TabPanelReporting$TabContainerReports$TabPanelFilingInquiries$ddlExport"] = "2";
formFields["TabContainerReportViewer$TabPanelReporting$TabContainerReports$TabPanelFilingInquiries$btnSubmitOptional"] = "Submit";
formFields["TabContainerReportViewer$TabPanelDownloads$TabContainerDownloads$TabPanelSelectiveFilings$txtCID"] = String.Empty;
formFields["TabContainerReportViewer$TabPanelDownloads$TabContainerDownloads$TabPanelSelectiveFilings$txtFilingOrg"] = String.Empty;
formFields["TabContainerReportViewer$TabPanelDownloads$TabContainerDownloads$TabPanelSelectiveFilings$ddlQuarter"] = "Pick";
formFields["TabContainerReportViewer$TabPanelDownloads$TabContainerDownloads$TabPanelSelectiveFilings$ddlDownloadType"] = "CSV";
formFields["TabContainerReportViewer$TabPanelDownloads$TabContainerDownloads$TabPanelSelectiveFilings$txtName"] = String.Empty;
formFields["TabContainerReportViewer$TabPanelDownloads$TabContainerDownloads$TabPanelSelectiveFilings$txtEmail"] = String.Empty;
formFields["__EVENTTARGET"] = String.Empty;
formFields["__EVENTARGUMENT"] = String.Empty;
formFields["__LASTFOCUS"] = String.Empty;
formFields["__AjaxControlToolkitCalendarCssLoaded"] = String.Empty;
formFields["TabContainerReportViewer_ClientState"] = "{\"ActiveTabIndex\" : 0,\"TabState\": [true,true]}";
formFields["TabContainerReportViewer_TabPanelReporting_TabContainerReports_ClientState"] = "{\"ActiveTabIndex\" : 1,\"TabState\": [true,true]}";
formFields["TabContainerReportViewer_TabPanelDownloads_TabContainerDownloads_ClientState"] = "{\"ActiveTabIndex\" : 0,\"TabState\": [true,true]}";
formFields["__VIEWSTATE"] = ViewState;
formFields["__VIEWSTATEGENERATOR"] = ViewStateGenerator;
formFields["__VIEWSTATEENCRYPTED"] = ViewStateEncrypted;
string postString = FormatPostString(formFields);
PostBackValue = postString;
}
}
private static void PostToPageForSubmitBtn()
{
HttpWebRequest objRequestPostPage = (HttpWebRequest)WebRequest.Create(AcquisitionURL);
objRequestPostPage.Method = WebRequestMethods.Http.Post;
objRequestPostPage.ContentLength = PostBackValue.Length;
objRequestPostPage.ContentType = "application/x-www-form-urlencoded";
objRequestPostPage.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
objRequestPostPage.KeepAlive = true;
objRequestPostPage.Host = "eqrreportviewer.ferc.gov";
objRequestPostPage.Headers.Add("Cache-Control", "max-age=0");
objRequestPostPage.Headers.Add("Sec-Fetch-Dest", "document");
objRequestPostPage.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36";
objRequestPostPage.Headers.Add("Origin", "https://eqrreportviewer.ferc.gov");
objRequestPostPage.Headers.Add("Sec-Fetch-Site", "same-origin");
objRequestPostPage.Headers.Add("Sec-Fetch-Mode", "navigate");
objRequestPostPage.Referer = "https://eqrreportviewer.ferc.gov/";
objRequestPostPage.Headers.Add("Accept-Encoding", "gzip, deflate,br");
objRequestPostPage.Headers.Add("Accept-Language", "en-US,en;q=0.9");
//Pass in the ASP.NET Session ID
objRequestPostPage.Headers.Add("Cookie", SessionID);
objRequestPostPage.Headers.Add("Upgrade-Insecure-Requests", "1");
objRequestPostPage.Headers.Add("Sec-Fetch-User", "?1");
objRequestPostPage.ServicePoint.Expect100Continue = false;
StreamWriter streamWriterPostPage = new StreamWriter(objRequestPostPage.GetRequestStream());
//Post the arguments
streamWriterPostPage.Write(PostBackValue);
streamWriterPostPage.Close();
//Get response
HttpWebResponse responsePostPage = (HttpWebResponse)objRequestPostPage.GetResponse();
WebHeaderCollection responseHeaders = responsePostPage.Headers;
Stream responseStream = responsePostPage.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
PageSourceCode = reader.ReadToEnd();
using (FileStream file = new FileStream(#"C:\Test\test.csv", FileMode.Create, FileAccess.Write))
{
WriteFile(responseStream, file);
}
}
Can anyone let me know if there is something wrong that I am doing. Right now all the values are hard coded but if it works I can organize that properly.
Also I don't get the Content Disposition response header in the response that I am getting but I get this header when its gets runned from Chrome browser.
What is the code that I can do differently or if I am missing something?
Any help/suggestion would be great help moving forward with this issue.
I was not able to do this using C#.
Finally I used python in combination with selenium and chrome web driver to get the task done.
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--disable-extensions")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
options.add_experimental_option("prefs", {"download.default_directory":"/databricks/driver"})
driver = webdriver.Chrome(chrome_options=options)
driver.implicitly_wait(5)
url = "https://eqrreportviewer.ferc.gov/"
driver.get(url)
driver.implicitly_wait(5)
#Filing Inquiries
driver.find_element_by_xpath('//*[#id="__tab_TabContainerReportViewer_TabPanelReporting_TabContainerReports_TabPanelFilingInquiries"]').click()
driver.implicitly_wait(5)
#Submission by Date
driver.find_element_by_xpath('//*[#id="TabContainerReportViewer_TabPanelReporting_TabContainerReports_TabPanelFilingInquiries_ddlReportType"]/option[5]').click()
driver.implicitly_wait(5)
#CSV
driver.find_element_by_xpath('//*[#id="TabContainerReportViewer_TabPanelReporting_TabContainerReports_TabPanelFilingInquiries_ddlExport"]/option[2]').click()
driver.implicitly_wait(15)
#Submit
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[#id="TabContainerReportViewer_TabPanelReporting_TabContainerReports_TabPanelFilingInquiries_btnSubmitOptional"]')))
element.click()
driver.implicitly_wait(15) #putting wait here to make sure file gets downloaded before driver is stopped.
driver.quit()

WebClient returning error 500 on GET request MVC 5

I'm having an issue with the WebClient. I'm using API-key authentication, and the API-key is indeed legit. If I turn off authentication on my API it gets the data without any issues, however, if I enable checking for "Authentication" in the WebClient header, it returns a 401 with a wrong/invalid API-key, but with a valid one it returns error 500.
Code with the WebClient:
public ActionResult Index()
{
using (var client = new WebClient())
{
client.Headers.Clear();
client.Headers.Add("Authorization", AppSettings.ApiKey());
client.Headers.Add("Host", "localhost");
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
var newsJson = client.DownloadString("http://localhost:59308/Api/ListNews");
var newsJsonDeserialized = JsonConvert.DeserializeObject<List<News>>(newsJson);
ViewData["NewsFeed"] = newsJsonDeserialized.ToList();
}
var dayMessages = new List<string> { "an astonishing", "an amazing", "a beautiful", "a gorgeous", "a loving" };
var randomNumber = new Random().Next(dayMessages.Count);
ViewData["DayOfWeekMsg"] = dayMessages[randomNumber] + " " + DateTime.Now.ToString("dddd");
return View();
}
ApiController:
public ActionResult ListNews()
{
using (var db = new Entities())
{
var apiToken = Request.Headers["Authorization"];
var apiTokens = db.ApiTokens.ToList();
var websiteUrl = Request.Url.Host;
if (apiTokens.SingleOrDefault(i => i.Token == apiToken && i.WebsiteUrl == websiteUrl) == null)
{
return new HttpUnauthorizedResult();
}
var news = db.News;
var newsList = news.ToList();
return Content(JsonConvert.SerializeObject(newsList, Formatting.Indented), "application/json");
}
}
Thanks in advance!
Not sure if it's the same problem but I was receiving a 500 and I fixed by setting the accept language header (with UserAgent):
client.Headers.Add(HttpRequestHeader.AcceptLanguage, "en");

View a PDF from SSRS / RDL

I want to access an RDL to obtain the report as PDF. I have
static void Main()
{
string pdfOutputFIleName = #"C:\Development\testoutputAsPdf.pdf";
var urlAsPdf = #"http://serverName/ReportServer/Reserved.ReportViewerWebControl.axd?ExecutionID=xxx&Culture=1033&CultureOverrides=False&UICulture=9&UICultureOverrides=False&ReportStack=1&ControlID=yyy&OpType=Export&FileName=Bug+Status&ContentDisposition=OnlyHtmlInline&Format=PDF";
var client = new WebClient();
client.UseDefaultCredentials = true;
//client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
client.Headers.Add("Content-Type", "application/pdf");
Process(client, urlAsPdf, pdfOutputFIleName);
Console.Read();
}
private static void Process(WebClient client, string url, string outputFileName)
{
Stream data = client.OpenRead(url);
using (var reader = new StreamReader(data))
{
string output = reader.ReadToEnd();
using (Stream s = File.Create(outputFileName))
{
var writer = new StreamWriter(s);
writer.Write(output);
Console.WriteLine(output);
}
}
}
The URL works fine in my browser. The program runs. When I to open the PDF I receive an error in Adobe:
There was an error opening this document. The file is damaged and
could not be repaired.
Since you already specify the output file name and the url to read from, perhaps you can use the WebClient.DownloadFile(string address, string filename) member.
private static void Process(WebClient client, string url, string outputFileName)
{
client.DownloadFile(url, outputFileName);
}

Requesting Definitions Using the Wordnik API

I've only worked with APIs in a very minimal sense so I've been wanting to try out how to do this for some time. Ok so this is what I have so far and it works but it returns everything of the definition. So I have a few questions:
Is there a way to request just the definitions without anything else?
Do I just parse the data? I saw in the Wordnik API and I can include XML tags...so can I use an XMLReader to grab the definitions?
Now how about requesting both the definitions and if it is noun/verb/etc at once?
The ultimate goal would be to create a list of definitions that I could do stuff with. Any help would be greatly appreciated. Here's my code so far:
class Program
{
static void Main(string[] args)
{
string apiKey = "***************";
string wordToSearch = "";
do
{
Console.Write("Please type a word to get the definition: ");
wordToSearch = Console.ReadLine();
if (!wordToSearch.Equals("q"))
{
string url = "http://api.wordnik.com/v4/word.json/" + wordToSearch + "/definitions?api_key=" + apiKey;
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/json";
using (WebResponse response = request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
string responseFromWordnik = reader.ReadToEnd();
Console.WriteLine(responseFromWordnik);
}
}
}
} while (!wordToSearch.Equals("q"));
}
}
thanks,
Justin
Here's an example to get definitions for a word. You need to replace the api key with your own api key.
public class Word
{
public string word { get; set; }
public string sourceDictionary { get; set; }
public string partOfSpeech { get; set; }
public string text { get; set; }
}
public class WordList
{
public List<Word> wordList { get; set; }
}
string url = "http://api.wordnik.com:80/v4/word.json/" + word + "/definitions?limit=200&includeRelated=false&sourceDictionaries=all&useCanonical=false&includeTags=false&api_key=a2a73e7b926c924fad7001ca3111acd55af2ffabf50eb4ae5";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = WebRequestMethods.Http.Get;
webRequest.Accept = "application/json";
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36";
webRequest.Referer = "http://developer.wordnik.com/docs.html";
webRequest.Headers.Add("Accept-Encoding", "gzip, deflate, sdch");
webRequest.Headers.Add("Accept-Language", "en-US,en;q=0.8");
webRequest.Host = "api.wordnik.com";
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
string enc = webResponse.ContentEncoding;
using (Stream stream = webResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
String responseString = "{\"wordList\":" + reader.ReadToEnd() + "}";
if (responseString != null)
{
JavaScriptSerializer ser = new JavaScriptSerializer();
WordList words = ser.Deserialize<WordList>(responseString);
}
}
The API documentation will probably tell you that.
Yes, parse the data. If the data is coming down as XML, then you can parse it with an XMLReader, or you can load it into an XMLDocument. It looks like you're asking for JSON, though. If so, you'll want a JSON parser. Check out Json.Net.
Again, check out the API documentation.
Their documentation page is suspiciously sparse. You'll probably get better response on their Google group or one of the other sources listed on their support page.

Categories