using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.IO; namespace testRsa
{
public class GetDataByHttp
{ public static string DoPost(string url, string data)
{
HttpWebRequest req = GetWebRequest(url, "POST");
byte[] postData = Encoding.UTF8.GetBytes(data);
Stream reqStream = req.GetRequestStream();
reqStream.Write(postData, , postData.Length);
reqStream.Close();
HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
return GetResponseAsString(rsp, encoding);
} public static HttpWebRequest GetWebRequest(string url, string method)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.ServicePoint.Expect100Continue = false;
req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
req.ContentType = "text/json";
req.Method = method;
req.KeepAlive = true;
req.UserAgent = "guanyisoft";
req.Timeout = ;
req.Proxy = null;
return req;
} public static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
{
StringBuilder result = new StringBuilder();
Stream stream = null;
StreamReader reader = null;
try
{
// 以字符流的方式读取HTTP响应
stream = rsp.GetResponseStream();
reader = new StreamReader(stream, encoding);
// 每次读取不大于256个字符,并写入字符串
char[] buffer = new char[];
int readBytes = ;
while ((readBytes = reader.Read(buffer, , buffer.Length)) > )
{
result.Append(buffer, , readBytes);
}
}
finally
{
// 释放资源
if (reader != null) reader.Close();
if (stream != null) stream.Close();
if (rsp != null) rsp.Close();
} return result.ToString();
} }
}
  

webservice 接口通过 HTTP 获取数据的更多相关文章

  1. 请求webservice接口的某方法数据

    NSURL *url = [NSURL URLWithString:@"http://xxx.xxx.com/xxx/xxxxWS?wsdl"]; NSString *soapMs ...

  2. CMDBuild安装及webservice接口的获取

    近期项目组之前一直使用的OneCMDB出现了问题,在增删改数据时异常的慢.于是考虑能否够优化OneCMDB.由于本人水平有限,对OneCMDB进行代码级别的优化临时还有点难度.于是就对现有的其它开源C ...

  3. java获取https网站证书,附带调用https:webservice接口

    一.java 获取https网站证书: 1.创建一个java工程,新建InstallCert类,将以下代码复制进去 package com; import java.io.BufferedReader ...

  4. 从底层获取接口url携带的数据

    从底层获取接口url携带的数据 //实例化HttpServletRequest HttpServletRequest request = ServletHolderFilter.getContext( ...

  5. java接口对接——别人调用我们接口获取数据

    java接口对接——别人调用我们接口获取数据,我们需要在我们系统中开发几个接口,给对方接口规范文档,包括访问我们的接口地址,以及入参名称和格式,还有我们的返回的状态的情况, 接口代码: package ...

  6. 从api接口获取数据-okhttp

    首先先介绍下api接口: API:应用程序接口(API:Application Program Interface) 通常用于数据连接,调用函数提供功能等等... 从api接口获取数据有四种方式:Ht ...

  7. Spring AOP 自定义注解获取http接口及WebService接口入参和出参

    注解方法实现过程中可以采用如下获取方式:—以下为例  HttpServletRequest request = ((ServletRequestAttributes) RequestContextHo ...

  8. Java之通过接口获取数据并用JDBC存储到数据库中

    最近做数据同步功能,从接口获取数据然后存到数据库中以便后续对数据进行相关操作,下面就贴一下相关代码. import com.alibaba.fastjson.JSON; import com.alib ...

  9. NodeJs本地搭建服务器,模拟接口请求,获取json数据

    最近在学习Node.js,虽然就感觉学了点皮毛,感觉这个语言还不错,并且也会一步步慢慢的学着的,这里实现下NodeJs本地搭建服务器,模拟接口请求,获取json数据. 具体的使用我就不写了,这个博客写 ...

随机推荐

  1. 将dataGridView数据转成DataTable

    如已绑定过数据源: DataTable dt = (dataGridView1.DataSource as DataTable) 如未绑定过数据源: public DataTable GetDgvTo ...

  2. [JavaScript]配置日期选择控件

    我选择的日期控件是:bootstrap-datepicker(下载路径:https://github.com/Aymkdn/Datepicker-for-Bootstrap) 比较方便,实用.原来是英 ...

  3. sql install error

    解决SQL Server 2008 R2安装过程中提示Could not open key的解决方法:以管理员身份运行CMD命令提示符,输入以下语句并运行就OK了secedit /configure ...

  4. android activity的启动方式

    1.Standard正常启动,默认的启动方式,没什么说头 2.SingleTop 意思就是在栈顶只能存在一个相同的activity 不能叠加,如果再A上继续启动A的话,只会调用A的onNewInten ...

  5. 一、OWIN初探

    前言 OWIN在.NET Web Servers与Web Application之间定义了一套标准接口,OWIN的目标是用于解耦Web Server和Web Application.基于此标准,鼓励开 ...

  6. CHARINDEX

    实现查询条件多个值的或的关系 Select Id,Name from CustTable where CharIndex( CustTable.Name, 'ACDE,BEX,CCC')>0 C ...

  7. Create a Listlink

    #ifndef List_h__ #define List_h__ #include <stdio.h> struct ListNode { int value; ListNode* pN ...

  8. 判断是手机还是PC端访问

    /*判断手机*/ //判断是手机浏览还是pc //平台.设备和操作系统 var system = { win: false, mac: false, xll: false, ipad: false } ...

  9. MacBook下如何安装mysql-python

    解决方法: 先把之前装的卸载干净:pip uninstall mysql-pythonbrew uninstall mysql-connector-c 现在设置下mysql_config路径:首先修改 ...

  10. JUC.Lock(锁机制)学习笔记[附详细源码解析]

    锁机制学习笔记 目录: CAS的意义 锁的一些基本原理 ReentrantLock的相关代码结构 两个重要的状态 I.AQS的state(int类型,32位) II.Node的waitStatus 获 ...