1. JAVA

String requestContent = "{"id":"1","sort":"des","number":"9","startIndex":"1"}";
try {
StringEntity entity = new StringEntity(requestContent);
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json"); //set the request content type as JSON
httpPost.setEntity(entity);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
httpResponse=httpClient.execute(httpPost);
2.C++
实现Http访问,微软提供了二套API:WinINet, WinHTTP, 他们实现Http访问的流程一致:
1, 首先我们打开一个Session获得一个HINTERNET session句柄;
2, 然后我们使用这个session句柄与服务器连接得到一个HINTERNET connect句柄;
3, 然后我们使用这个connect句柄来打开Http 请求得到一个HINTERNET request句柄;
4, 这时我们就可以使用这个request句柄来发送数据与读取从服务器返回的数据;
5, 最后依次关闭request,connect,session句柄。
#include "stdafx.h"
#include "windows.h"
#include <iostream>
#include <crtdbg.h>
#include <string>
#include <winhttp.h>
#pragma comment(lib, "winhttp")
using namespace std; void do_work(string strUrl);
int _tmain(int argc, _TCHAR* argv[])
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
do_work("https://albert.apple.com/WebObjects/ALUnbrick.woa/wa/deviceActivation?device=MacOS");
system("pause");
} void do_work(string strUrl)
{
DWORD dwSize = ;
DWORD dwDownloaded = ;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
//get the proxy
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ieProxyConfig = { };
if (!::WinHttpGetIEProxyConfigForCurrentUser(&ieProxyConfig))
{
// Call GetLastError for error information.
cout << "Error in getting proxy..." << endl;
}
WINHTTP_AUTOPROXY_OPTIONS autoProxyOptions = { };
if(ieProxyConfig.fAutoDetect)
{
autoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
autoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A;
} // Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen(L"A WinHTTP Example Program/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, ); // Specify an HTTP server
if (hSession)
hConnect = WinHttpConnect(hSession, L"baidu.com",
INTERNET_DEFAULT_HTTP_PORT, ); // Create an HTTP Request handle.
if (hConnect)
hRequest = WinHttpOpenRequest(hConnect, L"GET",
L"", //requestStr
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
); // Send a Request.
if (hRequest)
bResults = WinHttpSendRequest(hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
, WINHTTP_NO_REQUEST_DATA, ,
, ); // End the request.
if (bResults)
bResults = WinHttpReceiveResponse(hRequest, NULL); // Continue to verify data until there is nothing left.
if (bResults)
{
do
{
// Verify available data.
dwSize = ;
if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
printf("Error %u in WinHttpQueryDataAvailable.\n",
GetLastError()); // Allocate space for the buffer.
pszOutBuffer = new char[dwSize+]; // Read the Data.
ZeroMemory(pszOutBuffer, dwSize+); if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded))
printf("Error %u in WinHttpReadData.\n", GetLastError());
else
printf("%s\n", pszOutBuffer); // Free the memory allocated to the buffer.
delete[] pszOutBuffer;
} while (dwSize > );
} // Report errors.
if (!bResults)
printf("Error %d has occurred.\n", GetLastError()); // Close open handles.
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
}

各种语言使用HTTP Request的更多相关文章

  1. JSP 内置对象(request response session application out pageContext)

    request对象  javax.servlet.http.HttpServletRequest接口的实例 request.setCharacterEncoding("utf-8" ...

  2. Asp.net mvc 小试牛刀一:多语言支持

    最近因为项目需要又从UWP开发转到了Asp.net mvc 开发,由于也不是什么老手,所以就将项目常见的一些技术问题记录一下自己的解决方案. 第一个需求:用户可以自由切换界面显示语言. 解决方案一:界 ...

  3. Flask中request参数

    首先要明确一件事,Request这是个对象,不管使用PHP还是python还是什么java语言,虽然request这个对象可能叫的名字不一样,(在其他语言中可能叫什么HttpRequest),但是原理 ...

  4. .NET资源文件实现多语言切换

    1.创建对应的资源文件 lang.en.resx  英文 lang.resx   中文,默认 lang.zh-tw.resx  繁体 首先说明,这三个文件前面部分名称需要一样,只是 点 后面的语言代号 ...

  5. Struts2操作request、session和application对象

    Struts 2提供了多种方式来访问上述的三种对象,归结起来,可以划分为两大类:与Servlet API解耦的访问方式和与Servlet API耦合的访问方式. 与Servlet API解耦的访问方式 ...

  6. go语言爬虫goquery和grequests的使用

    /*下载工具*/ package main import ( "fmt" //go语言版本的jquery "github.com/PuerkitoBio/goquery& ...

  7. Request介绍及演示样例 PART1

    Request在ServletAPI的规范连接地址http://blog.csdn.net/zghwaicsdn/article/details/51035146 HTTP简介 URL是浏览器寻找信息 ...

  8. 关于在JSP页面中为什么一定要用${pageContext.request.contextPath}来获取项目路径,而不能用${request.contextPath}?

    这里的疑问在于pageContext和request都是JSP中的内置对象之一,为什么不直接用${request.contextPath}来获取项目路径? 出现这种疑问,其实是将JSP的内置对象和EL ...

  9. Web核心之Request对象

    HTTP协议中Request请求部分格式 //请求行(这种是POST类型的请求) POST /HttpServleLogin.html HTTP/1.1 //请求头(User-Agent里有Firef ...

随机推荐

  1. Spring IOC - 控制反转(依赖注入) - 创建对象的方式

    a.通过类的无参构造方法创建对象 在入门案例中就是这种方式.当用最普通的方式配饰一个<bean>时,默认就是采用类的 无参构造创建对象.在Spring容器初始化时,通过<bean&g ...

  2. restclient 访问 springmvc java工程接口

    一.tomcat和nginx 配置 /etc/nginx/conf.d/default.conf location /nsx{ proxy_pass http://nsx; proxy_connect ...

  3. linux 守护进程 daemon

    Linux的Service/Daemon你真的懂了吗? Linux 守护进程的启动方法 linux系统编程之进程(八):守护进程详解及创建,daemon()使用 linux守护进程 daemon 详解

  4. PostgreSQL下安装pg_stat_statements

    一.安装过程如下:进入postgreSQL安装包的contrib/pg_stat_statements目录,执行编译和安装动作:用root用户 make && make install ...

  5. Lua语言中的__index,__newindex,rawget和rawset

    转自:http://blog.csdn.net/wangbin_jxust/article/details/12108189 在谈及Lua中的__index,__newindex,rawget和raw ...

  6. Spark分析之DAGScheduler

    DAGScheduler概述:是一个面向Stage层面的调度器: 主要入参有: dagScheduler.runJob(rdd, cleanedFunc, partitions, callSite, ...

  7. 解决npm下载包失败的问题

    在我朝,用npm直接从官方的镜像下载包,经常会出现网络超时下载失败的问题,具体原因大家都懂,我就不说了. 不过,这些都无法阻挡我们对知识的渴望,一下提供几种我在工作中的解决办法,希望能帮助你. 1.安 ...

  8. 黏包的原理 (tcp udp) struct模块

    黏包 指数据混乱问题(发送端发送数据,接收端不知如何去接收) 只有tcp协议才会发送粘包,udp不会发生 黏包(tcp) 有一个合包机制(nagle算法),将多次连续发送且间隔较小的数据,进行打包成一 ...

  9. 过滤器 ;spring拦截器 切片 小结

    1. springMVc的拦截器 实现HandlerInterceptor接口,如下: public class HandlerInterceptor1 implements HandlerInter ...

  10. node 图片上传功能

    node 代码: var http = require("http"); var express = require('express') app = express(), for ...