POST数据到指定url并返回结果页面内容

using System.Net;
using System.Collections.Specialized;
public string getPostBackStream(string rUrl)
{
 // Create a new WebClient instance.
 WebClient myWebClient = new WebClient();
 // Create a new NameValueCollection instance to hold
 // some custom parameters to be posted to the URL.
 NameValueCollection myNameValueCollection = new NameValueCollection();
 myNameValueCollection.Add("title","this is title");    
 myNameValueCollection.Add("url","http://www.zl5.info/tb_client.aspx");
 myNameValueCollection.Add("excerpt","this is excerpt");
 byte[] responseArray = myWebClient.UploadValues(rUrl,"POST",myNameValueCollection);
 return Encoding.ASCII.GetString(responseArray);
}

转载于:https://www.cnblogs.com/ado-geek/archive/2012/05/17/2506240.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要在 `build.gradle` 文件中添加以下依赖: ```groovy implementation 'com.squareup.okhttp3:okhttp:4.9.1' ``` 接下来,创建一个包含用户名和密码的 `Login` 类: ```java public class Login { private String username; private String password; public Login(String username, String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } ``` 然后,创建一个 `LoginActivity` 类,继承自 `AppCompatActivity`,并使用 `OkHttpClient` 类发送 POST 请求。 ```java public class LoginActivity extends AppCompatActivity { private EditText editTextUsername; private EditText editTextPassword; private Button buttonLogin; private OkHttpClient client; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); editTextUsername = findViewById(R.id.edit_text_username); editTextPassword = findViewById(R.id.edit_text_password); buttonLogin = findViewById(R.id.button_login); client = new OkHttpClient(); buttonLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { login(); } }); } private void login() { String username = editTextUsername.getText().toString().trim(); String password = editTextPassword.getText().toString().trim(); if (TextUtils.isEmpty(username)) { editTextUsername.setError("用户名不能为空"); editTextUsername.requestFocus(); return; } if (TextUtils.isEmpty(password)) { editTextPassword.setError("密码不能为空"); editTextPassword.requestFocus(); return; } Login login = new Login(username, password); Gson gson = new Gson(); String json = gson.toJson(login); MediaType mediaType = MediaType.parse("application/json; charset=utf-8"); RequestBody requestBody = RequestBody.create(json, mediaType); Request request = new Request.Builder() .url("http://example.com/login") .post(requestBody) .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { String json = response.body().string(); Gson gson = new Gson(); User user = gson.fromJson(json, User.class); if (user != null) { Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); finish(); } else { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_LONG).show(); } }); } } }); } } ``` 在这个例子中,我们使用了 `Gson` 库将 `Login` 对象转换为 JSON 字符串,并使用 `MediaType` 类指定请求体的格式为 JSON。在响应的回调函数中,我们使用 `Gson` 库将 JSON 字符串转换为 `User` 对象,并根据返回的结果做出相应的操作。 注意:在实际应用中,您需要将 `http://example.com/login` 替换为您的服务器地址,同时需要根据您的数据模型调整 `Login` 和 `User` 类的属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值