如何使用WebRequest发送一些数据并读取响应?需要让服务器对API执行POST,如何将POST值添加到WebRequest对象以及如何发送它并获得响应(这将是一个字符串)?我需要发布两个值,有时更多,我在这些示例中看到它说stringpostData="Stringtopost";但是我如何让我发布的东西知道有多个表单值呢?来自MSDN//使用可以接收帖子的URL创建请求。WebRequestrequest=WebRequest.Create("http://contoso.com/PostAccepter.aspx");//将请求的方法属性设置为POST。request.Method="POST";//创建POST数据并将其转换为字节数组。stringpostData="这是一个将这个字符串发布到Web服务器的测试。";byte[]byteArray=Encoding.UTF8.GetBytes(postData);//设置WebRequest的ContentType属性。request.ContentType="application/x-www-form-urlencoded";//设置WebRequest的ContentLength属性。request.ContentLength=byteArray.Length;//获取请求流。流数据流=请求。获取请求流();//将数据写入请求流。数据流。写(由teArray,0,byteArray.Length);//关闭流对象。数据流.Close();//获取响应。WebResponse响应=请求。获取响应();//显示状态。安慰。WriteLine(((HttpWebResponse)response).StatusDescription);//获取包含服务器返回内容的流。数据流=响应。获取响应流();//使用StreamReader打开流以便于访问。StreamReaderreader=newStreamReader(dataStream);//读取内容。字符串responseFromServer=读者。读到结束();//显示内容。安慰。WriteLine(responseFromServer);//清理流。读者。关闭();数据流。关闭();回复。关闭();请注意,信息必须以key1=value1&key2=value2格式发送,这对我有用我确信它可以改进,所以请随时提出建议或编辑以使其更好。conststringWEBSERVICE_URL="http://localhost/projectname/ServiceName.svc/ServiceMethod";//此字符串未经测试,但我认为可以。字符串jsonData="{"key1":"value1","key2":"value2"}";尝试{varwebRequest=System.Net.WebRequest.Create(WEBSERVICE_URL);if(webRequest!=null){webRequest.Method="POST";webRequest.Timeout=20000;webRequest.ContentType="应用程序/json";使用(System.IO.Streams=webRequest.GetRequestStream()){使用(System.IO.StreamWritersw=newSystem.IO.StreamWriter(s))sw.Write(jsonData);}使用(System.IO.Streams=webRequest.GetResponse().GetResponseStream()){使用(System.IO.StreamReadersr=newSystem.IO.StreamReader(s)){varjsonResponse=sr.ReadToEnd();System.Diagnostics.Debug.WriteLine(String.Format("响应:{0}",jsonResponse));}}}}catch(Exceptionex){System.Diagnostics.Debug.WriteLine(ex.ToString());这是使用HttpWebRequest和HttpWebResponse对图像发布到Web服务的示例。StringBuildersb=newStringBuilder();字符串查询="?q="+纬度+"%2C"+经度+"&format=xml&key=xxxxxxxxxxxxxxxxxxxxxxxxxx";stringweatherservice="http://api.worldweatheronline.com/free/v1/marine.ashx"+查询;HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(weatherservice);request.Referer="http://www.yourdomain.com";HttpWebResponse响应=(HttpWebResponse)request.GetResponse();流流=response.GetResponseStream();StreamReaderreader=newStreamReader(stream);Char[]readBuffer=newChar[256];intcount=reader.Read(readBuffer,0,256);while(count>0){Stringoutput=newString(readBuffer,0,count);某人追加(输出);count=reader.Read(readBuffer,0,256);}字符串xml=sb.ToString();更强大的可以在这里找到,一个更灵活的例子:C#fileuploadwithformfields,cookieandheadersbelow是从文本文件中读取数据并将其发送到处理程序以处理和接收来自处理程序的响应数据并读取它并传递它的代码数据存储在字符串生成器类中的位置是上面的C#学习教程:如何使用WebRequest发布一些数据并读取响应?如果分享的内容对你有用,需要了解更多C#学习教程,希望大家多多关注——//从文本文件中获取需要发送的数据。FileStreamfileStream=newFileStream(@"G:Papertest.txt",FileMode.OpenOrCreate,FileAccess.ReadWrite);byte[]buffer=newbyte[fileStream.Length];intcount=fileStream.Read(buffer,0,buffer.Length);//这是一个处理程序,将接收数据并处理它并发回响应。WebRequestmyWebRequest=WebRequest.Create(@"http://localhost/Provider/ProcessorHandler.ashx");myWebRequest.ContentLength=buffer.Length;myWebRequest.ContentType="application/octet-stream";myWebRequest.Method="POST";//获取保存请求流的流对象。流stream=myWebRequest.GetRequestStream();stream.Write(buffer,0,buffer.Length);stream.Close();//发送网络请求并等待响应。尝试{WebResponsewebResponse=myWebRequest.GetResponse();//从响应中获取流数据StreamrespData=webResponse.GetResponseStream();//从流中读取响应。串流阅读器rstreamReader=newStreamReader(respData);字符串名称;StringBuilderstr=newStringBuilder();while((name=streamReader.ReadLine())!=null){str.Append(name);//当响应包含多行数据时添加到stringbuider}}catch(Exceptionex){throwex;}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
