LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-17-2017, 11:49 PM   #1
Prathameshkumar
LQ Newbie
 
Registered: Sep 2017
Posts: 3

Rep: Reputation: Disabled
hello i want to merge my GET and POST http request in one single program can any one help in doing this


-------------Program for GET request----------------
#include <stdio.h>
#include <stdlib.h>
#include <usr/mynetwork.h>

int
main(int argc, char** argv) {
int sockfd, n;
char buff[MAXLINE],recvline[MAXLINE];
struct sockaddr_in servaddr;

if(argc != 3){
printf("usage: httpgetconnect <ipaddress> <relative_url>");
exit(0);
}

if( (sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) == -1 ){
printf("socket: error");
exit(0);
}

bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(80); //connect to http server

if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr) < 0){
printf("port: assigned invalid");
exit(0);
}
if( connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)) == -1){
printf("connect: error");
exit(0);
}

//Sending Http header
bzero(&buff,sizeof(buff));

sprintf(buff,"GET %s HTTP1.1\nAccept: */*\nUser-Agent: Mozilla/4.0\nContent-Type: text/html\nPragma: no-cache\nConnection: keep-alive\n\n",argv[2]);
if(write(sockfd,buff,sizeof(buff)) == -1)
err_exit("write");

while ((n =read(sockfd,recvline,sizeof(recvline))) > 0){
recvline[n] = 0;
if(fputs(recvline,stdout) == EOF)
err_abort("read error");
}

return (EXIT_SUCCESS);
}

---------------Program for POST request--------------------
#include <stdio.h>
#include <stdlib.h>
#include <usr/mynetwork.h>

/*
*
*/
int
main(int argc, char** argv) {
int sockfd, n;
char buff[MAXLINE],recvline[MAXLINE];
struct sockaddr_in servaddr;

if(argc != 3){
printf("usage: httppostconnect <ipaddress> <relative_url>");
exit(0);
}

if( (sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) == -1 ){
printf("socket: error");
exit(0);
}

bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(80); //connect to http server

if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr) < 0){
printf("port: assigned invalid");
exit(0);
}
if( connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)) == -1){
printf("connect: error");
exit(0);
}

//Sending Http header
bzero(&buff,sizeof(buff));

char postdata[] = "param1=value1&param2=value"; /* Change param and value here */

sprintf(buff,"POST %s HTTP1.1\r\nAccept: */*\r\nReferer: <REFERER HERE>\r\nAccept-Language: en-us\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept-Encoding: gzip,deflate\r\nUser-Agent: Mozilla/4.0\r\nContent-Length: %d\r\nPragma: no-cache\r\nConnection: keep-alive\r\n\r\n%s",argv[2],strlen(postdata),postdata);

if(write(sockfd,buff,strlen(buff)+1) == -1)
err_exit("write");

while ((n =read(sockfd,recvline,sizeof(recvline))) > 0){
recvline[n] = 0;
if(fputs(recvline,stdout) == EOF)
err_abort("read error");
}
return (EXIT_SUCCESS);
}
 
Old 09-18-2017, 06:13 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Hello Prathameshkumar and welcome to LQ.

In the future please use [code][/code] tags to enclose your code, it separates it from you normal text and also retains the formatting of the code.

Also suggest you expand a bit more here on what you've tried. Members at LQ are volunteers and not paid support. Giving two clips of code, saying you wish to merge them, and not much more doesn't tell anyone what architecture you'd prefer the result to look like. Anyone can merge two functions with an if-else clause, however what do you wish it to do that will be additive beyond your simple question of combining? Did you wish to make a single function that does this using passing arguments to decide which actions to take?

Moving your question to the Programming forum for better exposure.
 
1 members found this post helpful.
Old 09-18-2017, 09:28 AM   #3
Prathameshkumar
LQ Newbie
 
Registered: Sep 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hello rtmistler,

I want to call http request i.e., GET and POST request using C program which helps in getting the data from the server and the client can post the data into the server(using JSON data format). As you mentioned, here I want to make a single function that does this using passing arguments to decide which actions to take.

Thanks and regards
Prathameshkumar
 
Old 09-18-2017, 10:06 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
So you can do this. What exactly is the problem? Are you saying that you cannot write the code?
 
Old 09-18-2017, 10:08 AM   #5
Prathameshkumar
LQ Newbie
 
Registered: Sep 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
Yes I am not able to merge the code i.e., I am not able to write GET and POST methods in one single C Program. Can You please post for the same. It woould help me alot.

Thank You
 
Old 09-18-2017, 10:23 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by Prathameshkumar View Post
Yes I am not able to merge the code i.e., I am not able to write GET and POST methods in one single C Program. Can You please post for the same. It woould help me alot.

Thank You
No for a couple of reasons:
  1. The members of LQ are volunteers and are not here to do your work for you
  2. The intent of LQ is that you are trying to solve a problem and people here help you to solve that problem, yourself, and we'll advise about what to do
  3. No one can write code where they do not know enough information about what you wish the higher level interface to this resultant function, so there is more information needed. However I get the impression that you are not able to provide any better description at this time.
  4. If you were to receive code, what exactly could, or would you do with it? Since you are demonstrating the lack of capability to edit code, then one wonders whether or not you understand how to compile source into a usable result. Also, what if there are bugs? How will you deal with that?
 
2 members found this post helpful.
Old 09-18-2017, 11:24 AM   #7
!!!
Member
 
Registered: Jan 2017
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 997

Rep: Reputation: 382Reputation: 382Reputation: 382Reputation: 382
This is the perfect opportunity for you to practice the essential life&job skill of: finding optimal web-search keywords!!!
I would start with: example c program http get post
Then add keywords to narrow it down. Use -word to eliminate things, e.g. -csharp
Try intitle:c Research: advanced google search operators

Life is 'no fun' if one asks others to "use the internet for me, so I don't have to learn to use the www"!!!

Enjoy and tell us what keywords you used in searching. Give us a copule links you found, with notes about what would be better for your desired goal. Then LQ people can guide & advise you, like the saying "teach a person to fish, and they will have food for life, but if you just give them a fish, they will eat for one day only".

rtmistler is a generous, caring, wise mentor, tho you might not realize that at first glance!!! Best wishes for success.

Edit addition: Here, it looks like they might be two different concepts. So maybe you could/would just append the two programs together, removing first return...} thru 2nd usage: ...}
IF I asked you: how do I combine these two commands into one, how would you answer?
Code:
ls .
df .
ls . ; df .

After you have made an attempt (tried something), come back for more help.
Tell us precisely what you tried, and clearly show how the results are not what you desire.

Last edited by !!!; 09-18-2017 at 12:17 PM.
 
Old 09-18-2017, 01:33 PM   #8
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

As rtmistler has already pointed out, it is not appropriate to ask others to write your code for you. Rather, you should try to work it out yourself and ask for help with specific problems when you cannot figure out how to do a particular thing.

In this case, if you already have working code for each use case and you wish to merge them into a single application which selects the intended use by command argument, you should be able to implement it almost as easily as you can write the description!

First, you need to detect the shell argument, argv, the user will pass to the application. A little research into those terms should show how that is commonly done. You might also want to look into getopt as a way to simplify argument handling.

Next, merge the code! As you already have working code for each case, that should be almost trivial!

If you get stuck somewhere along the way, reduce the problem to a simplest example which demonstrates the problem you are having and ask for help with that part of your code.

Good luck!
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
php curl http post request submit with a secure connection conflicker Programming 0 06-13-2012 12:31 PM
[SOLVED] Any program for inspecting http request for IE 6 (as httpfox for firefox)? Felipe Linux - Software 3 08-19-2011 09:26 AM
Unable to send HTTP POST request using PPP over GPRS harsh_electro Linux - Networking 7 07-25-2008 09:01 PM
HTTP REQUEST to Apache server from C program Balakrishnan84 Programming 6 07-26-2007 04:00 AM
HTTP request parsing: POST mofofish Programming 3 12-05-2003 10:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration