0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

dbpediaの研究 その30

Posted at

#概要

dbpediaが難解なので、手出してみる。
c,c++で、取得してみた。

#サンプルコード

#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netdb.h>
#include <unistd.h>

#define SA          struct sockaddr
#define MAXLINE     4096
#define MAXSUB      300
#define LISTENQ     1024
extern int h_errno;
ssize_t process_http(int sockfd, char *host, char *page, char *poststr) {
	char sendline[MAXLINE + 1], recvline[MAXLINE + 1];
	ssize_t n;
	snprintf(sendline, MAXSUB, "POST %s HTTP/1.0\r\n" "Host: %s\r\n" "Content-type: application/x-www-form-urlencoded\r\n" "Content-length: %d\r\n\r\n" "%s", page, host, strlen(poststr), poststr);
	write(sockfd, sendline, strlen(sendline));
	while ((n = read(sockfd, recvline, MAXLINE)) > 0) 
	{
		recvline[n] = '\0';
		printf("%s", recvline);
	}
	return n;
}
int main(void) {
	int sockfd;
	struct sockaddr_in servaddr;
	char **pptr;
	char *hname = "ja.dbpedia.org";
	char *page = "/sparql";
	char *poststr = "query=SELECT+DISTINCT+*+WHERE+%7B+dbpedia-ja%3A%E3%83%87%E3%83%BB%E3%83%88%E3%83%9E%E3%82%BD+dbpedia-owl%3Aabstract+%3Fabstract+.%7D";
	char str[50];
	struct hostent *hptr;
	if ((hptr = gethostbyname(hname)) == NULL) 
	{
		fprintf(stderr, " gethostbyname error for host: %s: %s", hname, hstrerror(h_errno));
		exit(1);
	}
	printf("hostname: %s\n", hptr->h_name);
	if (hptr->h_addrtype == AF_INET && (pptr = hptr->h_addr_list) != NULL)
	{
		printf("address: %s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
	}
	else 
	{
		fprintf(stderr, "Error call inet_ntop \n");
	}
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(80);
	inet_pton(AF_INET, str, &servaddr.sin_addr);
	connect(sockfd, (SA *) & servaddr, sizeof(servaddr));
	process_http(sockfd, hname, page, poststr);
	close(sockfd);
	exit(0);
}

#成果物

https://paiza.io/projects/AOpAtukbwavu25FBzpR8Gg
https://paiza.io/projects/GlN5CwW_GoTSR4oTvmnwKA

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?