Curl cmd and curl lib not yielding the same results
up vote
-1
down vote
favorite
im using curl lib to perform some webscraping on a swedish recipe database page(i have of course asked for permission). However im getting different results when i run curl in the commandline and curl lib with the same options.
curl "https://kokboken.ikv.uu.se/sok.php" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: sv-SE,sv;q=0.8,en-US;q=0.5,en;q=0.3" --compressed -H "Referer: https://kokboken.ikv.uu.se/sok.php" -H "Content-Type: application/x-www-form-urlencoded" -H "Connection: keep-alive" -H "Upgrade-Insecure-Requests: 1" --data "search_text=tomat&dummy=&search_type=all&rec_cats"%"5B"%"5D=all&submit_search=S"%"F6k&recid=&offset=0&searchid="
this curl command returns the correct response which is a html page with a list of recipes.
but when i run it in with the help of curllib(i use a c# wrapper called CurlThin) It returns the blank search page without any results as i my parameters where not passed. The expected behavior if i would enter the page via a browser.
C# code
private CURLcode _global;
private SafeEasyHandle _easy;
private readonly string HEMMETS_ROOT = "https://kokboken.ikv.uu.se/";
_global = CurlNative.Init();
// curl_easy_init() to create easy handle.
_easy = CurlNative.Easy.Init();
CurlNative.Easy.SetOpt(_easy, CURLoption.CAINFO, CurlResources.CaBundlePath);
CurlNative.Easy.SetOpt(_easy, CURLoption.USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0");
//Add headers to the request
var headers = CurlNative.Slist.Append(SafeSlistHandle.Null, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
// Add one more value to existing HTTP header list.
CurlNative.Slist.Append(headers, "Accept-Language: sv-SE,sv;q=0.8,en-US;q=0.5,en;q=0.3");
CurlNative.Slist.Append(headers, "Referer: https://kokboken.ikv.uu.se/sok.php");
CurlNative.Slist.Append(headers, "Content-Type: application/x-www-form-urlencoded");
CurlNative.Slist.Append(headers, "Connection: keep-alive");
CurlNative.Slist.Append(headers, "Upgrade-Insecure-Requests: 1");
// Configure libcurl easy handle to send HTTP headers we configured.
CurlNative.Easy.SetOpt(_easy, CURLoption.HTTPHEADER, headers.DangerousGetHandle());
CurlNative.Easy.SetOpt(_easy, CURLoption.URL, HEMMETS_ROOT + "sok.php");
var postData = "search_text=tomat&dummy=&search_type=all&rec_cats"%"5B"%"5D=all&submit_search=S"%"F6k&recid=&offset=0&searchid=";
// This one has to be called before setting COPYPOSTFIELDS.
CurlNative.Easy.SetOpt(_easy, CURLoption.POSTFIELDS, postData);
CurlNative.Easy.SetOpt(_easy, CURLoption.POST, 1);
CurlNative.Easy.SetOpt(_easy, CURLoption.SSL_VERIFYHOST, 0);
CurlNative.Easy.SetOpt(_easy, CURLoption.SSL_VERIFYPEER, 0);
var stream = new MemoryStream();
CurlNative.Easy.SetOpt(_easy, CURLoption.WRITEFUNCTION, (data, size, nmemb, user) =>
{
var length = (int)size * (int)nmemb;
var buffer = new byte[length];
Marshal.Copy(data, buffer, 0, length);
stream.Write(buffer, 0, length);
return (UIntPtr)length;
});
var result = CurlNative.Easy.Perform(_easy);
html = Encoding.GetEncoding(1252).GetString(stream.ToArray());
return html;
I Suspect A: that my request is not performing a post as it is behaving as i opened the site with a get request. Or B: That im missing some kind of curl option that the commandline specifies automatically.
Edit
I believe im using libcurl 1.1.1
c# asp.net curl web-scraping libcurl
|
show 5 more comments
up vote
-1
down vote
favorite
im using curl lib to perform some webscraping on a swedish recipe database page(i have of course asked for permission). However im getting different results when i run curl in the commandline and curl lib with the same options.
curl "https://kokboken.ikv.uu.se/sok.php" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: sv-SE,sv;q=0.8,en-US;q=0.5,en;q=0.3" --compressed -H "Referer: https://kokboken.ikv.uu.se/sok.php" -H "Content-Type: application/x-www-form-urlencoded" -H "Connection: keep-alive" -H "Upgrade-Insecure-Requests: 1" --data "search_text=tomat&dummy=&search_type=all&rec_cats"%"5B"%"5D=all&submit_search=S"%"F6k&recid=&offset=0&searchid="
this curl command returns the correct response which is a html page with a list of recipes.
but when i run it in with the help of curllib(i use a c# wrapper called CurlThin) It returns the blank search page without any results as i my parameters where not passed. The expected behavior if i would enter the page via a browser.
C# code
private CURLcode _global;
private SafeEasyHandle _easy;
private readonly string HEMMETS_ROOT = "https://kokboken.ikv.uu.se/";
_global = CurlNative.Init();
// curl_easy_init() to create easy handle.
_easy = CurlNative.Easy.Init();
CurlNative.Easy.SetOpt(_easy, CURLoption.CAINFO, CurlResources.CaBundlePath);
CurlNative.Easy.SetOpt(_easy, CURLoption.USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0");
//Add headers to the request
var headers = CurlNative.Slist.Append(SafeSlistHandle.Null, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
// Add one more value to existing HTTP header list.
CurlNative.Slist.Append(headers, "Accept-Language: sv-SE,sv;q=0.8,en-US;q=0.5,en;q=0.3");
CurlNative.Slist.Append(headers, "Referer: https://kokboken.ikv.uu.se/sok.php");
CurlNative.Slist.Append(headers, "Content-Type: application/x-www-form-urlencoded");
CurlNative.Slist.Append(headers, "Connection: keep-alive");
CurlNative.Slist.Append(headers, "Upgrade-Insecure-Requests: 1");
// Configure libcurl easy handle to send HTTP headers we configured.
CurlNative.Easy.SetOpt(_easy, CURLoption.HTTPHEADER, headers.DangerousGetHandle());
CurlNative.Easy.SetOpt(_easy, CURLoption.URL, HEMMETS_ROOT + "sok.php");
var postData = "search_text=tomat&dummy=&search_type=all&rec_cats"%"5B"%"5D=all&submit_search=S"%"F6k&recid=&offset=0&searchid=";
// This one has to be called before setting COPYPOSTFIELDS.
CurlNative.Easy.SetOpt(_easy, CURLoption.POSTFIELDS, postData);
CurlNative.Easy.SetOpt(_easy, CURLoption.POST, 1);
CurlNative.Easy.SetOpt(_easy, CURLoption.SSL_VERIFYHOST, 0);
CurlNative.Easy.SetOpt(_easy, CURLoption.SSL_VERIFYPEER, 0);
var stream = new MemoryStream();
CurlNative.Easy.SetOpt(_easy, CURLoption.WRITEFUNCTION, (data, size, nmemb, user) =>
{
var length = (int)size * (int)nmemb;
var buffer = new byte[length];
Marshal.Copy(data, buffer, 0, length);
stream.Write(buffer, 0, length);
return (UIntPtr)length;
});
var result = CurlNative.Easy.Perform(_easy);
html = Encoding.GetEncoding(1252).GetString(stream.ToArray());
return html;
I Suspect A: that my request is not performing a post as it is behaving as i opened the site with a get request. Or B: That im missing some kind of curl option that the commandline specifies automatically.
Edit
I believe im using libcurl 1.1.1
c# asp.net curl web-scraping libcurl
Is there any reason why you cannot simply use the built-in .NET HTTP libraries, which would make the code far simpler and almost certainly work without any mess or fuss?
– Ian Kemp
Nov 22 at 10:06
Don't you need to setCURLoption.POSTFIELDSIZEas per the example at github.com/stil/CurlThin#user-content-post-request ?
– Ian Kemp
Nov 22 at 10:08
I'm pretty sure that the problem is in the code where you are settingpostData
– Selvin
Nov 22 at 10:14
Your example is actually incomplete,postDatais not defined so we have no way of knowing whether it's a string, int, ... Please edit that info into the question, or we cannot help you.
– Ian Kemp
Nov 22 at 10:20
@IanKemp sorry i will edit it in
– scottyaim
Nov 22 at 10:22
|
show 5 more comments
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
im using curl lib to perform some webscraping on a swedish recipe database page(i have of course asked for permission). However im getting different results when i run curl in the commandline and curl lib with the same options.
curl "https://kokboken.ikv.uu.se/sok.php" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: sv-SE,sv;q=0.8,en-US;q=0.5,en;q=0.3" --compressed -H "Referer: https://kokboken.ikv.uu.se/sok.php" -H "Content-Type: application/x-www-form-urlencoded" -H "Connection: keep-alive" -H "Upgrade-Insecure-Requests: 1" --data "search_text=tomat&dummy=&search_type=all&rec_cats"%"5B"%"5D=all&submit_search=S"%"F6k&recid=&offset=0&searchid="
this curl command returns the correct response which is a html page with a list of recipes.
but when i run it in with the help of curllib(i use a c# wrapper called CurlThin) It returns the blank search page without any results as i my parameters where not passed. The expected behavior if i would enter the page via a browser.
C# code
private CURLcode _global;
private SafeEasyHandle _easy;
private readonly string HEMMETS_ROOT = "https://kokboken.ikv.uu.se/";
_global = CurlNative.Init();
// curl_easy_init() to create easy handle.
_easy = CurlNative.Easy.Init();
CurlNative.Easy.SetOpt(_easy, CURLoption.CAINFO, CurlResources.CaBundlePath);
CurlNative.Easy.SetOpt(_easy, CURLoption.USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0");
//Add headers to the request
var headers = CurlNative.Slist.Append(SafeSlistHandle.Null, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
// Add one more value to existing HTTP header list.
CurlNative.Slist.Append(headers, "Accept-Language: sv-SE,sv;q=0.8,en-US;q=0.5,en;q=0.3");
CurlNative.Slist.Append(headers, "Referer: https://kokboken.ikv.uu.se/sok.php");
CurlNative.Slist.Append(headers, "Content-Type: application/x-www-form-urlencoded");
CurlNative.Slist.Append(headers, "Connection: keep-alive");
CurlNative.Slist.Append(headers, "Upgrade-Insecure-Requests: 1");
// Configure libcurl easy handle to send HTTP headers we configured.
CurlNative.Easy.SetOpt(_easy, CURLoption.HTTPHEADER, headers.DangerousGetHandle());
CurlNative.Easy.SetOpt(_easy, CURLoption.URL, HEMMETS_ROOT + "sok.php");
var postData = "search_text=tomat&dummy=&search_type=all&rec_cats"%"5B"%"5D=all&submit_search=S"%"F6k&recid=&offset=0&searchid=";
// This one has to be called before setting COPYPOSTFIELDS.
CurlNative.Easy.SetOpt(_easy, CURLoption.POSTFIELDS, postData);
CurlNative.Easy.SetOpt(_easy, CURLoption.POST, 1);
CurlNative.Easy.SetOpt(_easy, CURLoption.SSL_VERIFYHOST, 0);
CurlNative.Easy.SetOpt(_easy, CURLoption.SSL_VERIFYPEER, 0);
var stream = new MemoryStream();
CurlNative.Easy.SetOpt(_easy, CURLoption.WRITEFUNCTION, (data, size, nmemb, user) =>
{
var length = (int)size * (int)nmemb;
var buffer = new byte[length];
Marshal.Copy(data, buffer, 0, length);
stream.Write(buffer, 0, length);
return (UIntPtr)length;
});
var result = CurlNative.Easy.Perform(_easy);
html = Encoding.GetEncoding(1252).GetString(stream.ToArray());
return html;
I Suspect A: that my request is not performing a post as it is behaving as i opened the site with a get request. Or B: That im missing some kind of curl option that the commandline specifies automatically.
Edit
I believe im using libcurl 1.1.1
c# asp.net curl web-scraping libcurl
im using curl lib to perform some webscraping on a swedish recipe database page(i have of course asked for permission). However im getting different results when i run curl in the commandline and curl lib with the same options.
curl "https://kokboken.ikv.uu.se/sok.php" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: sv-SE,sv;q=0.8,en-US;q=0.5,en;q=0.3" --compressed -H "Referer: https://kokboken.ikv.uu.se/sok.php" -H "Content-Type: application/x-www-form-urlencoded" -H "Connection: keep-alive" -H "Upgrade-Insecure-Requests: 1" --data "search_text=tomat&dummy=&search_type=all&rec_cats"%"5B"%"5D=all&submit_search=S"%"F6k&recid=&offset=0&searchid="
this curl command returns the correct response which is a html page with a list of recipes.
but when i run it in with the help of curllib(i use a c# wrapper called CurlThin) It returns the blank search page without any results as i my parameters where not passed. The expected behavior if i would enter the page via a browser.
C# code
private CURLcode _global;
private SafeEasyHandle _easy;
private readonly string HEMMETS_ROOT = "https://kokboken.ikv.uu.se/";
_global = CurlNative.Init();
// curl_easy_init() to create easy handle.
_easy = CurlNative.Easy.Init();
CurlNative.Easy.SetOpt(_easy, CURLoption.CAINFO, CurlResources.CaBundlePath);
CurlNative.Easy.SetOpt(_easy, CURLoption.USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0");
//Add headers to the request
var headers = CurlNative.Slist.Append(SafeSlistHandle.Null, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
// Add one more value to existing HTTP header list.
CurlNative.Slist.Append(headers, "Accept-Language: sv-SE,sv;q=0.8,en-US;q=0.5,en;q=0.3");
CurlNative.Slist.Append(headers, "Referer: https://kokboken.ikv.uu.se/sok.php");
CurlNative.Slist.Append(headers, "Content-Type: application/x-www-form-urlencoded");
CurlNative.Slist.Append(headers, "Connection: keep-alive");
CurlNative.Slist.Append(headers, "Upgrade-Insecure-Requests: 1");
// Configure libcurl easy handle to send HTTP headers we configured.
CurlNative.Easy.SetOpt(_easy, CURLoption.HTTPHEADER, headers.DangerousGetHandle());
CurlNative.Easy.SetOpt(_easy, CURLoption.URL, HEMMETS_ROOT + "sok.php");
var postData = "search_text=tomat&dummy=&search_type=all&rec_cats"%"5B"%"5D=all&submit_search=S"%"F6k&recid=&offset=0&searchid=";
// This one has to be called before setting COPYPOSTFIELDS.
CurlNative.Easy.SetOpt(_easy, CURLoption.POSTFIELDS, postData);
CurlNative.Easy.SetOpt(_easy, CURLoption.POST, 1);
CurlNative.Easy.SetOpt(_easy, CURLoption.SSL_VERIFYHOST, 0);
CurlNative.Easy.SetOpt(_easy, CURLoption.SSL_VERIFYPEER, 0);
var stream = new MemoryStream();
CurlNative.Easy.SetOpt(_easy, CURLoption.WRITEFUNCTION, (data, size, nmemb, user) =>
{
var length = (int)size * (int)nmemb;
var buffer = new byte[length];
Marshal.Copy(data, buffer, 0, length);
stream.Write(buffer, 0, length);
return (UIntPtr)length;
});
var result = CurlNative.Easy.Perform(_easy);
html = Encoding.GetEncoding(1252).GetString(stream.ToArray());
return html;
I Suspect A: that my request is not performing a post as it is behaving as i opened the site with a get request. Or B: That im missing some kind of curl option that the commandline specifies automatically.
Edit
I believe im using libcurl 1.1.1
c# asp.net curl web-scraping libcurl
c# asp.net curl web-scraping libcurl
edited Nov 22 at 10:23
asked Nov 22 at 10:00
scottyaim
1413
1413
Is there any reason why you cannot simply use the built-in .NET HTTP libraries, which would make the code far simpler and almost certainly work without any mess or fuss?
– Ian Kemp
Nov 22 at 10:06
Don't you need to setCURLoption.POSTFIELDSIZEas per the example at github.com/stil/CurlThin#user-content-post-request ?
– Ian Kemp
Nov 22 at 10:08
I'm pretty sure that the problem is in the code where you are settingpostData
– Selvin
Nov 22 at 10:14
Your example is actually incomplete,postDatais not defined so we have no way of knowing whether it's a string, int, ... Please edit that info into the question, or we cannot help you.
– Ian Kemp
Nov 22 at 10:20
@IanKemp sorry i will edit it in
– scottyaim
Nov 22 at 10:22
|
show 5 more comments
Is there any reason why you cannot simply use the built-in .NET HTTP libraries, which would make the code far simpler and almost certainly work without any mess or fuss?
– Ian Kemp
Nov 22 at 10:06
Don't you need to setCURLoption.POSTFIELDSIZEas per the example at github.com/stil/CurlThin#user-content-post-request ?
– Ian Kemp
Nov 22 at 10:08
I'm pretty sure that the problem is in the code where you are settingpostData
– Selvin
Nov 22 at 10:14
Your example is actually incomplete,postDatais not defined so we have no way of knowing whether it's a string, int, ... Please edit that info into the question, or we cannot help you.
– Ian Kemp
Nov 22 at 10:20
@IanKemp sorry i will edit it in
– scottyaim
Nov 22 at 10:22
Is there any reason why you cannot simply use the built-in .NET HTTP libraries, which would make the code far simpler and almost certainly work without any mess or fuss?
– Ian Kemp
Nov 22 at 10:06
Is there any reason why you cannot simply use the built-in .NET HTTP libraries, which would make the code far simpler and almost certainly work without any mess or fuss?
– Ian Kemp
Nov 22 at 10:06
Don't you need to set
CURLoption.POSTFIELDSIZE as per the example at github.com/stil/CurlThin#user-content-post-request ?– Ian Kemp
Nov 22 at 10:08
Don't you need to set
CURLoption.POSTFIELDSIZE as per the example at github.com/stil/CurlThin#user-content-post-request ?– Ian Kemp
Nov 22 at 10:08
I'm pretty sure that the problem is in the code where you are setting
postData– Selvin
Nov 22 at 10:14
I'm pretty sure that the problem is in the code where you are setting
postData– Selvin
Nov 22 at 10:14
Your example is actually incomplete,
postData is not defined so we have no way of knowing whether it's a string, int, ... Please edit that info into the question, or we cannot help you.– Ian Kemp
Nov 22 at 10:20
Your example is actually incomplete,
postData is not defined so we have no way of knowing whether it's a string, int, ... Please edit that info into the question, or we cannot help you.– Ian Kemp
Nov 22 at 10:20
@IanKemp sorry i will edit it in
– scottyaim
Nov 22 at 10:22
@IanKemp sorry i will edit it in
– scottyaim
Nov 22 at 10:22
|
show 5 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428338%2fcurl-cmd-and-curl-lib-not-yielding-the-same-results%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is there any reason why you cannot simply use the built-in .NET HTTP libraries, which would make the code far simpler and almost certainly work without any mess or fuss?
– Ian Kemp
Nov 22 at 10:06
Don't you need to set
CURLoption.POSTFIELDSIZEas per the example at github.com/stil/CurlThin#user-content-post-request ?– Ian Kemp
Nov 22 at 10:08
I'm pretty sure that the problem is in the code where you are setting
postData– Selvin
Nov 22 at 10:14
Your example is actually incomplete,
postDatais not defined so we have no way of knowing whether it's a string, int, ... Please edit that info into the question, or we cannot help you.– Ian Kemp
Nov 22 at 10:20
@IanKemp sorry i will edit it in
– scottyaim
Nov 22 at 10:22